【问题标题】:Codeigniter - insert batch - multiple inputs with same nameCodeigniter - 插入批处理 - 具有相同名称的多个输入
【发布时间】:2016-11-21 06:57:20
【问题描述】:

我正在尝试为批发商、经销商和客户插入一批行 注意:我使用“追加”来添加输入行,但它们带有相同的输入名称。 我正在尝试首先将其应用于批发,但没有发生 当我在我的批发输入列表中添加另一行时,它只需要一行(我的最后一行)输入,而我的另一行输入被丢弃。数组的 var_dump 揭示了这一点。

这是我的控制器文件

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

【问题讨论】:

    标签: mysql codeigniter


    【解决方案1】:

    在 for 循环中使用插入操作。如果您在所有字段中都获得了数组,那么您必须像在 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);
    }    
    

    【讨论】:

    • 我的数组 $range 没有检索到所有同名的输入字段
    • @Ramya 你想拥有多个范围吗?那么你应该使用输入作为数组名 [] 。就像你在价格上所做的那样。
    • 是的,但是当我尝试存储具有相同名称的输入值数组时,它不会发生!就像在我的代码中一样,当我 var_dump() 我的数组变量时,它只显示同名的最后一个输入!所以我尝试在每个输入名称后附加一个 id,尽管我做了一个漫长的过程,但它仍然有效!不过谢谢你
    • 好的,而且您还将插入函数放在 for 循环之外,这就是它只存储最后一行的原因。因为循环在最后一条记录结束。
    • 如果您对我的回答感到满意,那么您可以投票给我的回答。
    猜你喜欢
    • 2016-03-06
    • 2017-08-03
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 1970-01-01
    相关资源
    最近更新 更多