How to update a column value with the current column value plus an increment in codeigniter? [duplicate]

StackOverflow https://stackoverflow.com/questions/23664910

  •  22-07-2023
  •  | 
  •  

Question

I want to to update a column value with the current column value plus an increment. My table structure is as follows:

id   |   menu   | priority
__________________________

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

A simple query I have tried that is working:

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's not working.

Was it helpful?

Solution

According to docs

You may also use the $this->db->set() function when performing updates to the database.

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

set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE. To illustrate the difference, here is set() used both with and without the escape parameter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top