0
votes

I want to make an invoice form. For this reason i need to insert multiple data at once. for example I want to input db 5 category sale or purchase product at a time. But I am not interested with insert_batch. I try but i got some null value in db.

My Model is:

 <?php
 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<10; $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; }
      }}

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

My view for example:

  <?php echo form_label ('Price:'); ?>   
      <select name="price[]" id="price" class="input-xlarge">

      </select>



      <?php echo form_label ('Quantity'); ?> 

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

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

      <?php echo form_label ('Price:'); ?>   
           <select name="price[]" id="price2" class="input-xlarge">

           </select>

      <?php echo form_label ('Quantity'); ?> 

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

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

Please help.

1
Which values you get Null ? please specify !Hatem
There is no "vendor" element in your view ! please show full script of your view layer !Hatem

1 Answers

1
votes

The problem has been solved. My mistake in my model. The correct model is

public function purchase(){

$data = array();
$temp = count($this->input->post('quantity'));

 for($i=0; $i<$temp; $i++){

$invoice_no  =   $this->input->post('invoice');
$date        =   $this->input->post('date');
$price       =   $this->input->post('price');
$quantity    =   $this->input->post('quantity');
$vendor_name =   $this->input->post('vendor_name');
$model       =   $this->input->post('model');
  if(  $quantity[$i]!= '') {    
$data[] = array(
     'date'=>$date, 
     'vendor_name'=>$vendor_name[$i],
     'model'=>$model[$i],
     'price' =>$price[$i], 
     'purchase_quantity'=>$quantity[$i],
     'amount'  =>$price[$i]*$quantity[$i],
     'invoice_no'=>$invoice_no
     );} }

      $insert = count($data);

            if($insert)
            {
            $this->db->insert_batch('purchase', $data);
            }

            return $insert;
      } 

Thanks every one who try to help me.