phpcodeignitersql-updatelimitquery-builder

Codeigniter update table column with different values


I have the following records in my table:

id            status         

129           'Ready'           
129           'Ready'          
129           'Confirm'          
129           'Confirm'        
129           'Processing'        
130           'Confirm'        

What I am trying to change is change the status according to id based on user input.

I am capturing user input in my controller as below:

$id= $this->input->post("id");
$confirm = $this->input->post("confirm");
$processing = $this->input->post("processing");
$ready = $this->input->post("ready");
$delivered = $this->input->post("delivered");

an example of user input values is as follows:

   $id= 129
   $confirm = 1
   $processing = 2
   $ready = 2
   $delivered = 0

This basically means that all rows with id 129 should have status in the order, confirm should appear once, processing twice, ready ready and no delivered.

How can I update the status so that it would be like below using the above input data:

id            status 
129           'Ready'
129           'Ready' 
129           'Confirm'
129           'Processing' 
129           'Processing' 
130           'Confirm' 

In my model I tried as below but it's not working.

public function update_status($serialized,$confirm,$processing,$ready,$delivered) {
$data = array(
'status' => 'Confirm',  
);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($confirm);
            $this->db->update('items', $data);`$data = array(
'status' => 'Processing',   
);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($processing);
            $this->db->update('items', $data);
$data = array(
'status' => 'Ready',    
);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($ready);
            $this->db->update('items', $data);
$data = array(
'status' => 'Delivered',    
);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($delivered);
            return $this->db->update('items', $data); }

Any help would be highly appreciated.

`


Solution

  • Try this out:

    function update_status($id, $serialized, $confirm = 0, $processing = 0, $ready = 0, $delivered = 0) {
        if (count($confirm) > 0) {
            $this->db->where('id', $id);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($confirm);
            $this->db->update('items', array('status' => 'Confirm'));
        }
        if (count($processing) > 0) {
            $this->db->where('status', 'Confirm');
            $this->db->where('id', $id);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($processing);
            $this->db->update('items', array('status' => 'Processing'));
        }
        if (count($ready) > 0) {
            $this->db->where('status', 'Processing');
            $this->db->where('id', $id);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($ready);
            $this->db->update('items', array('status' => 'Ready'));
        }
        if (count($delivered) > 0) {
            $this->db->where('status', 'Ready');
            $this->db->where('id', $id);
            $this->db->where('is_serialized', $serialized);
            $this->db->limit($delivered);
            $this->db->update('items', array('status' => 'Delivered'));
        }
    }