How to update column value with current column value plus increment in codeigniter?

I want to update a column value with the current column value plus an increment. My table structure looks like this:

id   |   menu   | priority
__________________________

 1   |   One    |   2
 2   |   Two    |   3
 3   |   Three  |   1

      

A simple query I tried works:

update table set priority=priority+1 where id=2

      

in codeigniter:

$update_new_pri=$this->db->where('id',$menu_ids[$i])->update($this->table['menu'],array('menu_priority'=>'menu_priority+1'));

      

But it doesn't work.

+1


source to share


1 answer


According to the docs

You can also use the $ this-> db-> set () function when performing a database update.



$this->db->where('id',$menu_ids[$i])
     ->set('menu_priority', 'menu_priority+1', FALSE)      
     ->update($this->table['menu']);

      

set () will also accept an additional third parameter ($ escape), which will prevent data escaping if set to FALSE. To illustrate the difference, here is set (), used with and without escape Parameter.

+4


source







All Articles