1

My following Model is for insert array in db. I have also submit view file example. Please help to insert batch data in codeigniter with loop.

class Purchase_model extends CI_Model{

  public function purchase(){
  $price = $this->input->post('price');
  $quantity = $this->input->post('quantity');
  $date = $this->input->post('date');
     $vendor_name = $this->input->post('vendor_name');
     $model = $this->input->post('model');

    $invoice_no = $this->input->post('invoice');
    $temp = count($this->input->post('vendor_name'));
  for($i=0; $i<$temp; $i++){
  $data = array(
     'date'=>$date[$i], 
     'vendor_name'=>$vendor_name[$i],
     'model'=>$model[$i],
     'price' =>$price[$i], 
     'purchase_quantity'=>$quantity[$i],
     'amount'  =>$price[$i]*$quantity[$i],
     'invoice_no'=>$invoice_no[$i]
     );

   $insert = $this->db->insert('purchase',$data);
   return $insert; }
   }

But when I submit I get the following value in db

       Sl  date   Vendor name   model price quantity amount invoice
       89   2                      A     1    0            0       a

Please help. I need Multiple value insert with one form in db.

My view form look like

  <?php 
   $data = array ('name'      => 'quantity',
           'class'     =>'input-xlarge',
           'value'     => set_value('quantity')
           ); 

           ?>

           <?php echo form_input ($data); ?> 

My controller is

   public function purchase()
 { 
     if($this->Purchase_model->purchase()){

    $this->session->set_flashdata('Success', 'You are entered data  successfully');       
     redirect('home/purchase_form'); 
 }
 } 
3
  • Please show your controller code if possible.. Commented Sep 26, 2014 at 5:02
  • public function purchase(){ if($this->Purchase_model->purchase()){ $this->session->set_flashdata('Success', 'You are entered data successfully'); redirect('home/purchase_form'); } } Commented Sep 26, 2014 at 5:15
  • Edit your question and show full View code it's just single element you have shown. Commented Sep 26, 2014 at 5:21

2 Answers 2

1

You should use the insert_batch function instead, it should fix your problem and it is cleaner/more efficient as it requires only one query:

class Purchase_model extends CI_Model {

  public function purchase() {

     $price = $this->input->post('price');
     $quantity = $this->input->post('quantity');
     $date = $this->input->post('date');
     $vendor_name = $this->input->post('vendor_name');
     $model = $this->input->post('model');

     $invoice_no = $this->input->post('invoice');
     $count = count($this->input->post('vendor_name'));

     $insert_data = array();

     for($i=0; $i < $count; $i++){
         $data = array(
             'date'=>$date[$i], 
             'vendor_name'=>$vendor_name[$i],
             'model'=>$model[$i],
             'price' =>$price[$i], 
             'purchase_quantity'=>$quantity[$i],
             'amount'  =>$price[$i]*$quantity[$i],
             'invoice_no'=>$invoice_no[$i]
         );
         $insert_data[] = $data;
     }
     $insert = $this->db->insert_batch('purchase', $insert_data);
     return $insert;
  }

}
Sign up to request clarification or add additional context in comments.

3 Comments

Problem not solved. I got same figure in db. only showing first word.
@user3752230 It looks like it's a problem with $vendor_name[$i] then. Try doing die($vendor_name[$i]); in the loop to see if the vendor name variable contains what you want to be in the DB.
Actually do var_dump($vendor_name); instead (outside of the loop). It will output the array and you'll be able to see if the values are correct.
0

Try Following code

View Code

$data = array ('name'      => 'price[]',
    'class'     =>'input-xlarge',
    'value'     => set_value('price')
); 
echo form_input ($data);   

$data = array ('name'      => 'quantity[]',
    'class'     =>'input-xlarge',
    'value'     => set_value('quantity')
); 
echo form_input ($data);   

$data = array ('name'      => 'date[]',
    'class'     =>'input-xlarge',
    'value'     => set_value('date')
); 
echo form_input ($data);   

$data = array ('name'      => 'vendor_name[]',
    'class'     =>'input-xlarge',
    'value'     => set_value('vendor_name')
); 
echo form_input ($data);

$data = array ('name'      => 'model[]',
    'class'     =>'input-xlarge',
    'value'     => set_value('model')
); 
echo form_input ($data);

$data = array ('name'      => 'invoice[]',
    'class'     =>'input-xlarge',
    'value'     => set_value('invoice')
); 
echo form_input ($data); 

Model Code

class Purchase_model extends CI_Model {
  public function purchase() {

     $data_array = $this->input->post();

     $vendor_data   = $data_array['vendor_name'];

     foreach($vendor_data as $key => $value){
         $amount    =   $data_array['price'][$key] * $data_array['quantity'][$key];
         $data = array(
             'date'                 =>  $data_array['date'][$key], 
             'vendor_name'          =>  $data_array['vendor_name'][$key],
             'model'                =>  $data_array['model'][$key],
             'price'                =>  $data_array['price'][$key], 
             'purchase_quantity'    =>  $data_array['quantity'][$key],
             'amount'               =>  $amount,
             'invoice_no'           =>  $data_array['invoice'][$key]
         );
         $insert_data[] = $data;


    }
     $insert = $this->db->insert_batch('purchase', $insert_data);
     return $insert;


  }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.