mcprilla79
mcprilla79

Reputation: 99

Inserting an Array of data into Database in codeIgniter

I am trying to insert data from the dynamically text-fields which are created using jquery into the database. However only the first row of data is stored in the database and the others dont.i am unable to get why ,because i expect the loop to work on all data in the arrays. I do not know what i am doing wrong.

This is my View

<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> 

Script

$('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>');
            } 

 });});

Controller

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');
    }}

Model

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);
}

}

Upvotes: 0

Views: 453

Answers (1)

Pradeep
Pradeep

Reputation: 9717

Hope this will help you :

Remove return from the for loop before the $this->db->insert('sales',$data);

Your new_sale() method should be like this :

public function new_sale()
{
   $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]
            );
    $this->db->insert('sales',$data);
    }
}

Upvotes: 2

Related Questions