16

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.

1
  • Write echo $this->db->last_query();die; after this line : $this->db->update('po_order_details',$updateArray); //Could not update I don't know why to see what query is generated. Commented Aug 27, 2013 at 12:55

1 Answer 1

35

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'); 
Sign up to request clarification or add additional context in comments.

8 Comments

is the $this->db->update_batch('po_order_details',$updateArray, 'poid'); inside my for loop?
No sorry, it's after the for()
What if I want to have more than one field as a 'where' condition? like in additional to 'poid' also the result must match 'price'? *update x set f1 = d1 where f1 = cond1, f2 = cond2?
@Joraid I think in that case you should the standard update function so you can set multiple where
thanks, was hoping that update_batch could compare more than one field
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.