CodeIgniter Forums
Active Record Class: How to increment a field by one using update? - 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: Active Record Class: How to increment a field by one using update? (/showthread.php?tid=32156)



Active Record Class: How to increment a field by one using update? - El Forum - 07-14-2010

[eluser]nomikos3[/eluser]
This is the idea:

Code:
$this->db->update('files', array('hits' => 'hits+1'), array('id' => $id));

Is it possible?
Thanks,-


Active Record Class: How to increment a field by one using update? - El Forum - 07-14-2010

[eluser]nomikos3[/eluser]
I got it:

Checking "$this->db->set()"
in http://ellislab.com/codeigniter/user-guide/database/active_record.html
shows a suitable example with a field called "hits". So this is the way to do it:

Code:
$this->db->set('hits', 'hits+1', FALSE);
$this->db->where('id', $id);
$this->db->update('files');



Active Record Class: How to increment a field by one using update? - El Forum - 07-14-2010

[eluser]danmontgomery[/eluser]
Have you tried it? You will probably need to disable auto-escaping fields.


Active Record Class: How to increment a field by one using update? - El Forum - 07-14-2010

[eluser]nomikos3[/eluser]
Yes, The third parameter in db->set(), prevents data from being escaped if set to FALSE.
Cool and easy framework. Ah?
Thanks.-