13

大家好,我只是想问一下如何在 CodeIgniter 中使用数组执行批量更新这是我的示例代码:

 public function updateItemInfo(){

        $id = $this->input->post('idx'); //array of id
        $desc = $this->input->post('itemdesc'); //array of item name
        $qty = $this->input->post('qty'); //array or qty
        $price = $this->input->post('price'); //array of price
        $code = $this->input->post('codes'); // not array

        for($x = 0; $x < sizeof($id); $x++){

            $total[] = $price[$x] * $qty[$x];

            $updateArray = array(
                'item_desc' => $desc[$x],
                'item_qty' => $qty[$x],
                'price' => $price[$x],
                'total' => $total
            );
            $this->db->where('poid',$id[$x]);
            $this->db->update('po_order_details',$updateArray); //Could not update I don't know why

        }

        //echo "<pre>";
        //print_r($updateArray);


        $sumoftotal = array_sum($total);

        $vat_amt = $sumoftotal / 1.12;
        $vat_input = $vat_amt * 0.12;
        $total_all = $vat_amt + $vat_input;

        $updateTotal = array(
            'vatable_input' => $vat_amt,
            'vatable_amount' => $vat_input,
            'total_amount_due' => $total_all
        );

        //echo "<pre>";
        //print_r($updateTotal);

        //exit;

        $this->db->where('order_code',$code);
        $this->db->update('po_order_total',$updateTotal); //Here also couldn't update

    }

那是我的代码而且我不知道我的错误在哪里。Ia 还检查了我的数组值,我的数组中没有错误。我的问题是我无法使用批量更新来更新我的表。

4

1 回答 1

30

尝试在update_batch这里查看选项:https ://www.codeigniter.com/userguide2/database/active_record.html

CodeIgniter 3.x:http ://www.codeigniter.com/user_guide/database/query_builder.html?highlight=where#CI_DB_query_builder::update_batch

您可以使用所有选项创建一个array,然后将其发送到该batch_update函数。

$id = $this->input->post('idx'); //array of id
$desc = $this->input->post('itemdesc'); //array of item name
$qty = $this->input->post('qty'); //array or qty
$price = $this->input->post('price'); //array of price
$code = $this->input->post('codes'); // not array

$updateArray = array();

for($x = 0; $x < sizeof($id); $x++){

    $total[] = $price[$x] * $qty[$x];
    $updateArray[] = array(
        'poid'=>$id[$x],
        'item_desc' => $desc[$x],
        'item_qty' => $qty[$x],
        'price' => $price[$x],
        'total' => $total
    );
}      
$this->db->update_batch('po_order_details',$updateArray, 'poid'); 
于 2013-08-27T14:38:00.060 回答