CodeIgniter Forums
Increase for db->update possible? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Increase for db->update possible? (/showthread.php?tid=4132)



Increase for db->update possible? - El Forum - 11-08-2007

[eluser]tirithen[/eluser]
Is it possible to replace -INCREASE- in the code lines bellow to automaticly increase the value on that post?

Code:
this->db->where('id', $_POST['entry_id']);
$this->db->update('entries', array('num_comments' => -INCREASE- ));

And is it possible to decrease a value in the same manner?


Increase for db->update possible? - El Forum - 11-08-2007

[eluser]xwero[/eluser]
You can try
Code:
$this->db->update('entries', array('num_comments' => 'num_comments+1' ));
The full sql is this
Code:
$this->db->query('update enteries set num_comments=num_comments+1 where id=?',array($_POST['entry_id']));



Increase for db->update possible? - El Forum - 11-08-2007

[eluser]tirithen[/eluser]
I tried it but could not get i to work. For now I'll use this

Code:
$this->db->where('id', $_POST['entry_id'], 1);
$query = $this->db->get('entries');
$row = $query->row();
        
$this->db->where('id', $_POST['entry_id'], 1);
$this->db->update('entries', array('num_comments' => $row->num_comments + 1));



Increase for db->update possible? - El Forum - 11-08-2007

[eluser]xwero[/eluser]
I think it's best to use the query method version than to do another query to get the comments number.
And if you insist on using the first query use
Code:
$this->db->select('num_comments');
to limit the query result.


Increase for db->update possible? - El Forum - 11-08-2007

[eluser]Phil Sturgeon[/eluser]
There are probably a few ways of doing this with AR, but not all will work and to save me having to test any you could use the one I use. Why use 2 queries when you can use 1?

Code:
$this->db->set('views', 'views + 1');
$this->db->where('entryID', $this->entryID);
$this->db->update('Whatever');



Increase for db->update possible? - El Forum - 11-08-2007

[eluser]tirithen[/eluser]
Thanks for all the help. :-D
I'm getting more and more on how to write the code. :-)