I have got a foreach loop iterating over metalname array, which is nested inside a for loop, the foreach loop is coming from a seperate array, the issue is, during first iteration of for loop, the for each loop prints the values, but during next iteration of for loop, the foreach prints nothing.
whereas, for rest of the fields, there is no problem, they get printed on all iteratins.
$product = GetProduct();
$metal_name = get_metal();
$count = count($product);
<?php for($i=0;$i<$count;$i++) { ?>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2">Choose Metal</label>
<div class="col-lg-10">
<select class="form-control input-lg" name="metal_name[]">
<option value=''>Select Metal Name</option>
<?php
foreach ($metal_name as $r) { ?>
<option value="<?php echo $r['metal_id'];?>" <?php if($r['metal_id'] == $product[$i]['metal']){echo "selected='selected'";}?>><?php echo ucfirst($r['metal_name']);?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Product ID</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_id[]" value="<?php echo $product[$i]['id'];?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Charges Per Gram</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="gram_price[]" value="<?php echo $product[$i]['per_gram_price'];?>">
</div>
</div>
<?php } ?>
I found one thing on your forth line:
<?php for($i=0;$i<$count;$i++) { ?>
You didn't close out of PHP in your code above when you are getting your product and such. So this will give you some error.
In terms of your loop, a while loop is better for database work.
Here, I fixed it up for you. There were a few typos:
<?php
$product = GetProduct();
$metal_name = get_metal();
$count = count($product);
or($i=0;$i<$count;$i++) { ?>
<div class="form-group">
<label class="col-sm-2 control-label col-lg-2">Choose Metal</label>
<div class="col-lg-10">
<select class="form-control input-lg" name="metal_name[]">
<option value=''>Select Metal Name</option>
<?php
foreach ($metal_name as $r) {
?>
<option value="
<?php
echo $r['metal_id'];
if($r['metal_id'] == $product[$i]['metal']){
echo "selected='selected'";
echo ucfirst($r['metal_name']);
}
?>
</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Product ID</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="product_id[]" value="<?php echo $product[$i]['id'];?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Charges Per Gram</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="gram_price[]" value="<?php echo $product[$i]['per_gram_price']; ?>">
</div>
</div>
I fixed up your code a bit for you, try this out.
Related
Screenshot of the form without PHP:
When I add following PHP code:
<div class="col-lg-4 col-md-6 col-sm-12">
<label for="">Distributor Price</label>
<input type="text" name="DisPrice" placeholder="Distributor Price" class="form-control" required>
</div>
<div class="col-lg-4 col-md-12 col-sm-12">
<label for="">Brand Name</label>
<select name="car_id" id="car_id" class="form-control-label" >
<?php
$list = mysqli_query($conn, "SELECT * FROM `vehicle_registration` where `status`='0'");
while ($row_ah = mysqli_fetch_assoc($list)) {
?>
<option value="<?php echo $row_ah['id']; ?>"><?php echo $row_ah['car_no']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<label for="">Description (use for ||| new Line)</label>
<textarea name="Description" class="form-control" id="" cols="30" rows="10"></textarea>
</div>
After that my form looks like this:
Some CSS Logics still works but still show me the same issue.
You should inspect the HTML page using the inspect tools.
The error might be displayed there..
It could be from $conn variable.
I have a form which contain multiple file upload field ,I have success to upload multiple files in /upload folder.
But now when I edit the form that time I want to show the previously upload files as selected files where I can change the files,add new files or delete the files.
echo form_open_multipart(site_url()."lead/update/$lead->id");
?>
<div class="box-body">
<div class="form-group">
<label for="InputFirstName">First name</label>
<input type="text" class="form-control" id="InputFirstName" name="InputFirstName" placeholder="Enter first name" value="<?php echo $lead->fname; ?>">
</div>
<div class="form-group">
<label for="InputLastName">Last name</label>
<input type="text" class="form-control" id="InputLastName" name="InputLastName" placeholder="Enter last name" value="<?php echo $lead->lname; ?>">
</div>
<div class="form-group">
<label for="InputEmail1">Email address</label>
<input type="email" class="form-control" id="InputEmail1" name="InputEmail1" placeholder="Enter email" value="<?php echo $lead->email; ?>">
<?php echo form_error('InputEmail1', '<span class="help-block">', '</span>'); ?>
</div>
<div class="form-group">
<label for="InputContact">Contact number</label>
<input type="text" class="form-control" id="InputContact" name="InputContact" placeholder="Enter contact number" value="<?php echo $lead->phone; ?>">
</div>
<div class="form-group">
<label for="InputSource">Source</lable>
<select name="source" id="InputSource" class="form-control">
<option value="Email"<?php if($lead->source=='Email') echo 'selected="selected"';?>>Email</option>
<option value="Phone"<?php if($lead->source=='Phone') echo 'selected="selected"';?>>Phone</option>
<option value="Website"<?php if($lead->source=='Website') echo 'selected="selected"';?>>Website</option>
</select>
</div>
<div class="form-group">
<label for="InputStatus">Status</label>
<select name="status" id="InputStatus" class="form-control">
<option value="Draft"<?php if($lead->status=='Draft') echo 'selected="selected"';?>>Draft</option>
<option value="In Progress" <?php if($lead->status=='In Progress') echo 'selected="selected"';?>>In Progress</option>
<option value="Responsible Assigned" <?php if($lead->status=='Responsible Assigned') echo 'selected="selected"';?>>Responsible Assigned</option>
</select>
</div>
<div class="form-group">
<label for="exampleInputFile">Add Attachments</label>
<input type="file" id="files" name="files[]">
<div class="contents"></div>
<div>
Add more
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<?php echo form_submit(array('value'=>'Submit', 'class'=>'btn btn-primary btn_add', 'name'=>'SAVE')); ?>
</div>
<?php echo form_close(); ?>
This is my code
I want to generate the salary after user selects the designation dropdown value from the database.
my db strucuture is as follows:
employee:
emp_id dep_id
1 1
2 1
designation:
dep_id des_name salary
1 accountant 20000
This is my html code:
<div class="form-group">
<label class="control-label col-lg-2" for="initial">Designation :</label>
<div class="col-lg-3">
<select class="form-control input-sm" name="classification" id="classification" onchange="changeValue();">
<?php while($row3 = mysqli_fetch_array($result3)):; ?>
<option><?php echo $row3[2]; ?></option>
<?php endwhile; ?>
</select>
<span class="error_form" id="classification_error_message"></span>
</div>
<label class="control-label col-lg-2" for="basicSalary">Basic Salary :</label>
<div class="col-lg-3">
<input type="text" class="form-control input-sm" name="basicSalary" id="basicSalary" placeholder="0.00">
</div>
</div>
You can do this by using this code butt this code from php codeigniter. Now u can changing it according to your requirements.
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">select Categories <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select class="form-control" name="cat" id="catogires">
<!-- <option value="">Select categories</option>-->
<?php
$data=$this->Product_Model->cat_select();
foreach($data as $row)
{ ?>
<option <?php if ($product->cat_id==$row->cat_id) echo "selected='selected'"; ?> value="<?php echo $row->cat_id?>"><?php echo $row->cat_name;?></option>
<?php
}
?>
</select>
</div>
</div>
i have this html form
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Please Input Below Information</h3>
</div>
<!-- /.box-header -->
<!-- form start -->
<form method="POST" action="model.php" class="form-horizontal">
<div class="box-body">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Model Code *</label>
<div class="col-sm-10">
<input type="text" name="ModelCode" class="form-control" id="inputEmail3" placeholder="e.g KTI">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Model Name *</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="ModelName" id="inputPassword3" placeholder="e.g 0002">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Units *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelUnit" style="width: 100%;">
<option value="" selected disabled>Choose Model Unit</option>
<?php
$sql = mysqli_query($con, "select departmentname from department");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['departmentname']; ?>"><?php echo $row['departmentname']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Hour Theroy *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelHourT" style="width: 100%;">
<option value="" selected disabled>Choose Model Hour</option>
<?php
$sql = mysqli_query($con, "select GroupTeory from grouptheory");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['GroupTeory']; ?>"><?php echo $row['GroupTeory']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Model Hour Practice *</label>
<div class="col-sm-10">
<select class="form-control" name="ModelHourP" style="width: 100%;">
<option value="" selected disabled>Choose Model Practice</option>
<?php
$sql = mysqli_query($con, "select GroupPractical from grouppractical");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['GroupPractical']; ?>"><?php echo $row['GroupPractical']; ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Model Type *</label>
<div class="col-sm-10">
<input type="text" name="ModelType" class="form-control" id="inputEmail3" placeholder="e.g KTI">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Semister *</label>
<div class="col-sm-10">
<select class="form-control" style="width: 100%;" name="SemsterID">
<option value="" selected disabled>Choose Model Type</option>
<?php
$sql = mysqli_query($con, "select Semester from semester");
while ($row = mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $row['Semester']; ?>"><?php echo $row['Semester']; ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<input type="submit" name="submit" class="btn btn-info pull-right" value="Submit" />
</div>
<!-- /.box-footer -->
</form>
</div>
and this is my php insert function
include("Connection.php");
if(isset($_POST['submit']))
{
echo "<script>alert($query1)</script>";
$ModelCode = mysqli_real_escape_string($con,$_POST['ModelCode']);
$ModelName = mysqli_real_escape_string($con,$_POST['ModelName']);
$ModelUnit = mysqli_real_escape_string($con,$_POST['ModelUnit']);
$ModelHourT =mysqli_real_escape_string($con,$_POST['ModelHourT']);
$ModelHourP = mysqli_real_escape_string($con,$_POST['ModelHourP']);
$ModelType = mysqli_real_escape_string($con,$_POST['ModelType']);
$SemsterID = mysqli_real_escape_string($con,$_POST['SemsterID']);
$query1 = mysqli_query($con, "INSERT INTO 'model' (ModelCode,ModelName,ModelUnit,ModelHourTheory,ModelHourPractical,ModelType,StageID) VALUES ('$ModelCode','$ModelName','$ModelUnit','$ModelHourT','$ModelHourP','$ModelType' ,'$SemsterID')");
echo "<script>alert($query1)</script>";
}
and it doesn't work .... i didn't no what's happen !! even i have been compare fields of the table with the name of the database ... but it still doesn't work ... anyone knows what is problem ?
You don't should use single quote for table name (in this case single quote is for literal value )
Assuming that you table is named model you should use
$query1 = mysqli_query($con,
"INSERT INTO model (ModelCode, ModelName,ModelUnit,
ModelHourTheory,ModelHourPractical,ModelType,StageID)
VALUES ('$ModelCode','$ModelName','$ModelUnit',
'$ModelHourT','$ModelHourP','$ModelType','$SemsterID')");
I try to insert data from a form into a csv file the insert work fine but i have a problem with the new line i did different search and have different solution but nothing work for me
this is the code
$file = fopen('contacts.csv', 'a+');
// open the file and check for a character
if( fread($file, 1) != "" ){
// file has data, write new line
fwrite($file, "\n");
}else{
// file doesnt have data, no new line needed
}
$csv=array();
$csv[] = $_POST['txt_number'];
$csv[] = $_POST['txt_stage'];
$csv[] = $_POST['txt_category'];
$csv[] = $_POST['txt_description'];
$csv[] = $_POST['txt_division'];
$csv[] = $_POST['txt_notapprove'];
$csv[] = $_POST['txt_approve'];
$csv[] = $_POST['txt_notexist'];
$csv[] = $_POST['txt_priority'];
$csv[] = $_POST['txt_frequency'];
$csv[] = $_POST['startdate'];
$csv[] = $_POST['enddate'];
$csv[] = $_POST['duration'];
$csv[] = $_POST['txt_asd'];
$csv[] = $_POST['txt_add'];
$csv[] = $_POST['txt_aduration'];
$csv[] = $_POST['txt_transferredto'];
$csv[] = $_POST['txt_wo'];
$csv[] = $_POST['txt_percentage'];
$csv[] = $_POST['txt_note'];
// put data stored in $csv into file
fputcsv($file,$csv);
fclose($file);
the out put i want in the file is like this example
,,,test,test,,,,,, new line for a new date
,,,test2,test2,,,,,
i used different thing "\n","\r","\n\r"
but this solution didn't work
once it give me the result with new line without cama "," and that what i don't want
than how to solve this problem to a new line in a csv file in the look i want ?
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Description</label>
<div class="col-sm-3">
<input id="textinput" name="txt_description" type="text" placeholder="Description" class="form-control input-md">
</div>
<label class="col-sm-3 control-label" for="textinput">Division</label>
<div class="col-sm-3">
<input id="textinput" name="txt_division" type="text" placeholder="Division" class="form-control input-md" >
</div></div>
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Stage</label>
<div class="col-sm-3">
<input id="textinput" name="txt_stage" type="text" placeholder="Stage" class="form-control input-md" >
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="textinput">Category</label>
<div class="col-sm-3">
<input id="textinput" name="txt_category" type="text" placeholder="Category" class="form-control input-md" >
</div>
<label class="col-md-3 control-label" for="radios">Priority</label>
<div class="col-sm-3">
<select id="selectbasic" name="txt_priority" class="form-control">
<option value=""></option>
<option value="Low Priority">Low Priority</option>
<option value="Routine">Routine</option>
<option value="Urgent">Urgent</option>
<option value="Important">Important</option>
</select>
</div>
</div>
<label class="col-sm-3 control-label" for="textinput">Frequency</label>
<div class="col-sm-3">
<select id="selectbasic" name="txt_frequency" class="form-control">
<option value=""></option>
<option value="Once">Once</option>
<option value="Daily">Daily</option>
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
<option value="Semiannual">Semiannual</option>
</select>
</div>
<label class="col-sm-3 control-label" for="textinput">Transferred To</label>
<div class="col-sm-3">
<?php
$q=mysqli_query($conn,"SELECT concat(db_fname,' ' , db_lname) as fname from tbl_user where db_level!='4'
UNION
SELECT db_CompanyName as scn from tbl_supplier
UNION
SELECT db_CompanyName as sucn from tbl_subcontractor
")or die(mysqli_error($conn));
echo'<select name="txt_transferredto" class="form-control inpu-md" >';
echo"<option value=''></option>";
while($row=mysqli_fetch_array($q)){
$fname=$row['fname'];
$companyname=$row['scn'];
$subcompanyname=$row['sucn'];
if($fname!=""){
echo"<option value='$fname'>";echo $fname;echo"</option>";}
else if($subcompanyname!=""){
echo"<option value='$subcompanyname'>";echo $subcompanyname;echo"</option>";}
else if($companyname!=""){
echo"<option value='$companyname'>";echo $companyname;echo"</option>";}
}
echo'</select>';
?>
</div>
<label class="col-sm-3 control-label" for="textinput">WO</label>
<div class="col-sm-3">
<input id="textinput" name="txt_wo" type="text" placeholder="WO" class="form-control input-md">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="checkbox">Not Approve</label>
<div class="col-sm-3">
<input type = 'Radio' Name ='exist' value= 'Not Approve' >
</div>
<label class="col-sm-4 control-label" for="checkbox">Approve</label>
<div class="col-sm-3">
<input type = 'Radio' Name ='exist' value= 'Approve'>
</div>
<label class="col-sm-4 control-label" for="checkbox">Not Exist</label>
<div class="col-sm-3">
<input type = 'Radio' Name ='exist' value= 'Not Exist' >
</div>
<label class="col-sm-3 control-label" for="textinput">Percentage</label>
<div class="col-sm-3">
<input id="textinput" name="txt_percentage" type="text" placeholder="Percentage" class="form-control input-md">
</div>
</div>
<!--Planing Date-->
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Planing Start Date</label>
<div class="col-sm-3">
<input id="basic_example_1" name="startdate" type="text" placeholder="planing Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onChange="onDateChange()" onchange="cal()">
</div>
<label class="col-sm-3 control-label" for="textinput">Planing DUE Date</label>
<div class="col-sm-3">
<input id="basic_example_2" name="enddate" type="text" placeholder="planing Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onchange="cal()" >
</div>
<label class="col-sm-3 control-label" for="textinput" >Duration</label>
<div class="col-sm-3">
<input id="duration" name="duration" type="text" placeholder="Duration" class="form-control input-md" onChange="onDateChange()" >
</div>
</div>
<!--Actual Date -->
<div class="form-group">
<label class="col-sm-3 control-label" for="textinput">Actual Start Date</label>
<div class="col-sm-3">
<input id="basic_example_3" name="txt_asd" type="text" placeholder="Actual Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onChange="onDateChange()" onchange="call()" >
</div>
<label class="col-sm-3 control-label" for="textinput">Actual DUE Date</label>
<div class="col-sm-3">
<input id="basic_example_4" name="txt_add" type="text" placeholder="Actual Date" class="form-control input-md" onfocus="(this.type='date')" onblur="(this.type='text')" onchange="call()" >
</div>
<label class="col-sm-3 control-label" for="textinput" >Duration</label>
<div class="col-sm-3">
<input id="aduration" name="txt_aduration" type="text" placeholder="Duration" class="form-control input-md" onChange="onDateChange()" >
</div>
</div>
<!--progress Bar-->
<!-- Textarea -->
<div class="form-group">
<label class="col-md-3 control-label" for="textarea">Notes</label>
<div class="col-md-9">
<textarea class="form-control" id="textarea" name="txt_note" ></textarea>
</div>
</div>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-3 control-label" for="button1id"></label>
<div class="col-md-8">
<input type="submit" name="submit" value="Save" class="btn btn-success">
</div>
Format your CSV as multi-dimensional array. First level index means row index, second level index means column index.
$csv = [
[
'test',
'test',
'test',
],
[
'test2',
'test2',
'test2',
]
];
To write to CSV use loop and write line by line:
$file = fopen('contacts.csv', 'a+');
foreach ($csv as $row) {
fputcsv($file, $row);
}
fclose($file);
Than you will get output as
test,test,test
test2,test2,test2
Unless you are needing the fields in a different order in the csv than they appear in the form or I have missed the point here then the following works - every time the form is submitted it writes a new line with the $_POST data.
$filename = __DIR__ . '/contacts.csv';
$file = fopen( $filename, 'a+' );
fputcsv( $file, $_POST );
fclose( $file );
To test that each entry is on a new line, after the above code try adding:
$lines=file( $filename );
foreach( $lines as $line )echo $line.'<br />';
That shows each line being rendered with a br tag afterwards so you can assume that the line endings are being recognised correctly and that you should therefore be able to use that to enter to the db.
An alternative method that generates each $_POST on a new line that DOES appear correctly in Notepad.
$filename=__DIR__.'/contacts.csv';
file_put_contents( $filename, '"' . implode( '","', $_POST ). '"' . PHP_EOL, FILE_APPEND | FILE_TEXT );
the solution is this code work don't use notepad to see the result if you want you can also use PHP_EOL it work on notepad and other editor thanks for all for your response or can you use without PHP_EOL or \n