2016-11-21 73 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