2016-11-21 66 views
0

我正在尝试为批发,经销商和客户插入一批行 注意:我使用'append'添加一行输入,但它们带有相同的输入名称。 我试图将此应用于批发第一,但它不会发生 当我添加另一行到我的批发输入列表它只采取一行(我的最后一行)输入和我的其他行输入被丢弃。数组的var_dump显示。Codeigniter - 插入批处理 - 多个输入同名

这是我的控制器文件

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

这是我的模型文件:

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

回答

1

使用插入操作中用于loop.and如果你在所有的领域得到了数组,那么你必须使用像for循环中的索引数组,因为你说你只有一个输出范围,所以不要在循环中使用索引数组。

$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); 
}  
+0

我的数组$ range没有检索所有输入字段的同名 – Ramya

+0

@Ramya你想有多个范围?那么你应该使用输入作为数组名[]。就像你在价格上做的一样。 –

+0

是的但是当我尝试存储输入值的数组相同的名称它不会发生!就像在我的代码中,当我var_dump()我的数组变量它显示的是同名的最后一个输入!所以我试图给每个输入名称添加一个id,并且它通过一个很长的过程来实现它!谢谢 – Ramya