0

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

1 Answer 1

1

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

5 Comments

my array $range is not retrieving all the input fields with same name
@Ramya you want to have range as multiple ? then you should use input as array name [] . like you did in price .
yeah but when i try to store the array of input values with same name it is not happening! Like in my code when i var_dump() my array variable it is showing just the last input of the same name! So i tried to append a id to each input name and it worked though its a long process i made it ! thank you though
okay , and you also put insert function outside the for loop so, that's why its stored last row only . because loops ends at last record.
if you satisfied with my ans then you can up vote to my ans .

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.