Я пытаюсь вставить данные из динамически текстовых полей, которые создаются с помощью jquery, в базу данных. Однако в базе данных хранится только первая строка данных, а остальные нет. Я не могу понять почему, потому что я ожидаю, что цикл будет работать со всеми данными в массивах. Я не знаю, что я делаю неправильно.
Это мой взгляд
<tbody id="item_body">
<tr id="item_row">
<th>1</th>
<td>
<select class="form-control" name="item_name[]" id="item_name" style=" width:150px;">
<option value="none" selected="" disabled="">Select Item</option>
<?php foreach ($items as $item):?>
<option value="<?php echo $item['id'];?>"><?php echo $item['item_name']; ?> </option>
<?php endforeach;?>
</select>
</td>
<td><input type="text" name="price_p_u[]" id="price_p_u" class="form-control"></td>
<td><input type="text" name="qty_unit[]" id="qty_unit" class="form-control"></td>
<td>
<select name="unit-type[]" id="unit-type" class="form-control" style=" width:90px;">
<option value="none" selected="" disabled="">Choose</option>
<option value="Unit">Unit</option>
<option value="Sub-Unit">Sub-Unit</option>
</select>
</td>
<td><input type="text" name="qty[]" id="qty" class="form-control"></td>
<td><input type="text" name="total_price[]" id="total_price" class="form-control"></td>
<td><input type="button" name="" id="add_fields" class="btn btn-success" value="+"></td>
</tr>
</tbody>
<tbody id="total_part">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>Total</td>
<td id="grand_total">GH0.00</td>
</tr>
</tbody>
Сценарий
$('td #add_fields').click(function(){
item_count++;
var url = baseURL+'interfaces/population';
$.ajax({
type: "GET",
url: url,
data:'',
dataType: 'json',
success: function(res){
$('#item_body').append('<tr id="item_row_add'+item_count+'">'+
'<th>'+item_count+'</th>'+
'<td>'+
'<select class="form-control" name="item_name[]" id="item_name'+item_count+'" style=" width:150px;">'+
'<option value="none" selected="" disabled="">Select Item</option>');
for(i in res){
$('#item_row_add'+item_count+' select').append('<option value="'+res[i].id+'">'+res[i].item_name+'</option>');}
// alert(res[i].item_name);
$('#item_row_add'+item_count+'').append('</select>'+
'</td>'+
'<td><input type="text" name="price_p_u[]" id="price_p_u'+item_count+'" class="form-control"></td>'+
'<td><input type="text" name="qty_unit[]" id="qty_unit'+item_count+'" class="form-control"></td>'+
'<td>'+
'<select name="unit-type[]" id="unit-type'+item_count+'" class="form-control" style=" width:90px;">'+
'<option value="none" selected="" disabled="">Choose</option>'+
'<option value="Unit">Unit</option>'+
'<option value="Sub-Unit">Sub-Unit</option>'+
'</select>'+
'</td>'+
'<td><input type="text" name="qty[]" id="qty'+item_count+'" class="form-control"></td>'+
'<td><input type="text" name="total_price[]" id="total_price'+item_count+'" class="form-control"></td>'+
'<td><input type="button" name="" class="btn btn-danger remove_fields" value="X" ></td>'+
'</tr>');
}
});});
Контроллер
public function new_sale(){
$data['title']='Add sale';
$this->form_validation->set_rules('sales_date', 'Date', 'required');
$this->form_validation->set_rules('name_customer', 'Name of Customer', 'required');
$data['id']=$this->input->post('item_name');
$data['items']=$this->interface_model->get_item_data();
$data['lastInvoiceNo']=$this->interface_model->check_invoice_no();
if($this->form_validation->run()==FALSE){
$this->load->view('templates/header_interfaces');
$this->load->view('interfaces/new_sale', $data);
$this->load->view('templates/footer_interfaces');
}else{
$this->interface_model->new_sale();
$this->session->set_flashdata('sales_registered','Sale Recorded');
redirect('interfaces/new_sale');
}}
Модель
public function new_sale(){
//$item_id=$this->input->post('item_name');
$sales_date=$this->input->post('sales_date');
$name_customer=$this->input->post('name_customer');
$invoice_no=$this->input->post('invoice_no');
$item_id=$this->input->post('item_name');
$unit_type=$this->input->post('unit-type');
$qty=$this->input->post('qty');
$r_total_price=$this->input->post('total_price');
for($i=0; $i<count($r_total_price); $i++){
$data=array('sales_date'=>$sales_date,
'name_customer'=>$name_customer,
'invoice_no'=>$invoice_no,
'item_id'=>$item_id[$i],
'unit-type'=>$unit_type[$i],
'qty'=>$qty[$i],
'r_total_price'=>$r_total_price[$i]);
return $this->db->insert('sales',$data);
}
}