如何使用 Codeigniter 数组执行批量更新? [英] How to perform a batch update using Codeigniter arrays?

查看:27
本文介绍了如何使用 Codeigniter 数组执行批量更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 公共函数 updateItemInfo(){$id = $this->input->post('idx');//id数组$desc = $this->input->post('itemdesc');//项目名称数组$qty = $this->input->post('qty');//数组或数量$price = $this->input->post('price');//价格数组$code = $this->input->post('codes');//不是数组for($x = 0; $x < sizeof($id); $x++){$total[] = $price[$x] * $qty[$x];$updateArray = 数组('item_desc' =>$desc[$x],'item_qty' =>$qty[$x],'价格' =>$价格[$x],'总计' =>总计$);$this->db->where('poid',$id[$x]);$this->db->update('po_order_details',$updateArray);//无法更新我不知道为什么}//echo "

";//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 = 数组('vatable_input' =>$vat_amt,'vatable_amount' =>$vat_input,'total_amount_due' =>$total_all);//echo "

";//print_r($updateTotal);//出口;$this->db->where('order_code',$code);$this->db->update('po_order_total',$updateTotal);//这里也不能更新}

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

解决方案

尝试在此处查看 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');//id数组$desc = $this->input->post('itemdesc');//项目名称数组$qty = $this->input->post('qty');//数组或数量$price = $this->input->post('price');//价格数组$code = $this->input->post('codes');//不是数组$updateArray = array();for($x = 0; $x < sizeof($id); $x++){$total[] = $price[$x] * $qty[$x];$updateArray[] = 数组('poid'=>$id[$x],'item_desc' =>$desc[$x],'item_qty' =>$qty[$x],'价格' =>$价格[$x],'总计' =>总计$);}$this->db->update_batch('po_order_details',$updateArray, 'poid');

Hello guys I just want to ask how can I perform a batch update using arrays in CodeIgniter Here's my sample code:

 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

    }

That's my code And I can't figured it out where's my error. Ia also checked my array values and there's no error in my array. My problem is I can't update my table using batch update.

解决方案

Try to see update_batch option here: 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

You can create an array with all your option and then send it to the batch_update function.

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

这篇关于如何使用 Codeigniter 数组执行批量更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆