0

What i want is, i have a uniq id call num i want insert mutiple description for that unique num. i have a form to add fileds dynamically so i can add fields as much i want. when i try to insert one row data its works perfectly when i try to insert more than one row data it doesn't work.

My View page :

<form name="codexworld_frm" action="" method="post">

<div class="field_wrapper">
<input type="text" name="num" value=""/><br />

<input type="text" name="description[]" value=""/><input type="text" name="voucher_no[]" value=""/><input type="text" name="price[]" value=""/>
        <a href="javascript:void(0);" class="add_button" title="Add field"><img src="<?php echo base_url('images/add-icon.png'); ?>"/></a>

</div>
<input type="submit" name="submit" value="SUBMIT"/>
</form>

My Controller :

$data = array(
                'no' => $this->input->post('num'),
                'descriptions' => $this->input->post('description'),
                'voucher' => $this->input->post('voucher_no'),
                'des_price' => $this->input->post('price'),
            );

            $this->multi_model->form_insert($data);

            $data['message'] = 'Data Inserted Successfully';
            //Loading View
            $this->load->view('multi_view', $data);

My Model:

function form_insert($data){

    $this->db->insert('tbl_description', $data);
    }

if i use foreache loop into my model i think it will work but i how do i use ?

When i use print_r() function this is out put

    1001
Array
    (
        [0] => description1
        [1] => description2
        [2] => description3
    )
    Array
    (
        [0] => voucher 1
        [1] => voucher 2
        [2] => voucher 3
    )
    Array
    (
        [0] => 100
        [1] => 200
        [2] => 300
    )
Hamelraj
  • 4,676
  • 4
  • 19
  • 42

4 Answers4

6

see this link it is use multiple insert without loop or you can use your foreach loop in controller count description post and go through it.

    $data = array();

    $count = count($this->input->post['description']);

    for($i=0; $i < $count; $i++) {
        $data[] = array(
            'no'=>$this->input->post('num'),
            'descriptions' => $this->input->post['descriptions'][$i],
            'voucher' => $this->input->post['voucher'][$i],
            'des_price' => $this->input->post['des_price'][$i],
           );
    }
    $this->db->insert_batch('tbl_description', $data);
Community
  • 1
  • 1
Nikunj Chotaliya
  • 802
  • 8
  • 19
  • in this link only have two arrays ryt but i want add arrays dynamically it will b 1 or 2 or 3 or........etc – Hamelraj Jul 23 '15 at 05:28
  • you can print_r($_POST['description']) and see the result array use for it in foreach loop and other post are same it is and you are done for multiple insert using foreach loop. – Nikunj Chotaliya Jul 23 '15 at 05:33
1

Hope this will helps you..

Controller

//if voucher_no is required..

$voucher_no = $this->input->post('voucher_no');    
$count = count($voucher_no);
if ($count > 0) {
   for ($i = 0; $i < $count; $i++) {
       if (!empty($voucher_no[$i])) {
          $data = array(
            'no' => $this->input->post('num'),
            'descriptions' => $this->input->post('description')[$i],
            'voucher' => $this->input->post('voucher_no')[$i],
            'des_price' => $this->input->post('price')[$i],
            );

          $this->multi_model->form_insert($data);
        }
      } 
   }

 $data['message'] = 'Data Inserted Successfully';
 //Loading View
 $this->load->view('multi_view', $data);

Let us know the results..

1

change model as following.

function form_insert($data){
    foreach($data['description'] as $key=>$des)
    {
      $savedata = array(
            'no' => $data('no'),
            'descriptions' => $des,
            'voucher' => $data['voucher'][$key],
            'des_price' => $data['dec_price'][$key],
        );
      $this->db->insert('tbl_description', $savedata);
     }
}
shankar kumar
  • 648
  • 5
  • 9
1
you can insert object wise.

in your controller:-

$value=$this->input->post('num');
$count=count($val);
for($i=0; $i<$count; $i++){

$data['no']=$this->input->post('num')[$i];
$data['description']=$this->input->post('description')[$i];
$data['voucher']=$this->input->post('voucher')[$i];
$data['prize']=$this->input->post('prize')[$i];

$this->multi_model->form_insert($data);
}
Shahnad S
  • 983
  • 10
  • 17