简体   繁体   中英

How to insert multiple invoice values in codeigniter

I have a form with below layout in codeigniter:

<input type="text" name="product">`<input type="text" name="cost">`

The form has several rows with the same input names. Have tried several suggestions such as Batch creation and this thread here but not working out

<input type="text" name="product[]">
<input type="text" name="cost[]">

Controller:

function insertData() {
    $product = $this->input->post('product');
    $cost = $this->input->post('cost');

    foreach($product as $key=>$val){

     $data = array(
        'product'     =>$val,
        'cost'  =>$cost[$key]

        );

    }
   $this->db->insert_batch('table_name', $data); 
}

Use array of same textboxes name for batch insert

insert_batch function insert multiple data at a time to table in codeigniter

view page

<form action="<?=base_url('Test_c/insert_data')?>" method="post">
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <div class="col-sm-12">
                <input type="text" name="product[]"><input type="text" name="cost[]">
            </div>
            <input type="submit" name="submit" value="submit">
        </form>

controller function

function insert_data() {
            $product = $this->input->post('product');
            $cost = $this->input->post('cost');
            $insert_array = array();
            for ($i=0; $i < count($product); $i++) {
                $tmp = array();
                $tmp['product'] = $product[$i];
                $tmp['cost'] = $cost[$i];
                $insert_array[] = $tmp;
            }

            $this->db->insert_batch('test', $insert_array);
            //echo $this->db->last_query();
    }

You need to create a post array like so:

<input type="text" name="product[]">
<input type="text" name="cost[]">

Then you can foreach your post and insert_batch or several normal insert();

View

 <input type="text" name="product[]"><input type="text" name="cost[]">

Controller

$arrayOne = $this->input->post('product'); //array(1,2,4);
$arrayTwo = $this->input->post('cost'); //array(22,44,55);
$c = array_combine($arrayOne,$arrayTwo ); // output:array(1=>22,2=>44,4=>55)

foreach($c as $key=>$val){

  $data = array(
     'product_id' => $key ,
     'cost' => $val 
  );

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM