How to perform a batch update using Codeigniter arrays?

前端 未结 1 1370
轻奢々
轻奢々 2020-12-06 11:00

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(){

               


        
相关标签:
1条回答
  • 2020-12-06 11:36

    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'); 
    
    0 讨论(0)
提交回复
热议问题