Ramya Ramya - 13 days ago 12
MySQL Question

Codeigniter - insert batch - multiple inputs with same name

I am trying to insert a batch of rows for wholesale, dealer and customer
Note : i am using 'append' to add row of inputs but then they carry the same input names.
I am trying to apply this to wholesale first but it is not happening
when i add another row to my wholesale input list it is taking only one row(my last row) input and my other row of inputs are discarded. the var_dump of array reveals that.

This is my controller file

$range = $this->input->post('wrange1');
$usertype = $this->input->post('wusertype');
$uom = $this->input->post('wuom1');
$price = $this->input->post('wamount1'); //array of price
$vat = $this->input->post('wvat1');
var_dump($range);// i am getting only one output
$updateArray = [];
for($x = 0; $x < sizeof($price); $x++){
$updateArray[$x] = array(
'product_id'=> $id,
'range' => $range[$x],
'usertype' => $usertype[$x],
'uom' => $uom[$x],
'price' => $price[$x],
'vat' => $vat[$x]
);
}
$this->productmodel->updatepricinglist($updateArray);


This is my model file:

public function updatepricinglist($updateArray) {
$this->db->insert_batch('product_pricing', $updateArray);
}

Answer

use insert operation inside for loop.and if you get the array in all the field then you have to use array of index like in for loop , since you said you get only one range at output then don't use array of index in loop.

$range = $this->input->post('wrange1');
$usertype = $this->input->post('wusertype'); 
$uom = $this->input->post('wuom1'); 
$price = $this->input->post('wamount1'); //array of price
$vat = $this->input->post('wvat1'); 
var_dump($range);// i am getting only one output

for($x = 0; $x < sizeof($price); $x++){
$updateArray = array(
    'product_id'=> $id,
    'range' => $range,
    'usertype' => $usertype[$x],
    'uom' => $uom[$x],
    'price' => $price[$x],
    'vat' => $vat[$x]
);
 $this->productmodel->updatepricinglist($updateArray);
}    
Comments