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
    )
4
  • you can use $this->db->insert_batch(); Commented Jul 23, 2015 at 5:03
  • that also dsnt work :( Commented Jul 23, 2015 at 5:07
  • you want to insert only description multi times or whole $data ? Commented Jul 23, 2015 at 5:15
  • num is id for description so every descrption has voucher_no and price Commented Jul 23, 2015 at 5:21

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

in this link only have two arrays ryt but i want add arrays dynamically it will b 1 or 2 or 3 or........etc
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.
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 Comment

num is my primary key every description has vocher_no and price
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);
     }
}

Comments

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

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.