CodeIgniter Forums
Increment decimal value with active record - 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: Increment decimal value with active record (/showthread.php?tid=41249)



Increment decimal value with active record - El Forum - 05-03-2011

[eluser]Acidrain[/eluser]
Hey,

I can't get it to work.
The value in the database does not increment.

Code:
$myFloat = 45.83;
$id = 4;

$editData = array(
    'column_name' => 'column_name + ' . $myFloat
);

$this->db->where('id_column', $id);
$this->db->update('table_name', $editData);

Am I doing something wrong?

column is DECIMAL(20,5)


Increment decimal value with active record - El Forum - 05-03-2011

[eluser]hccoder[/eluser]
Hi,

Try the $this->db->set() method.

$this->db->where('id', $id);
$this->db->set('field', 'field+1');
$this->db->update('mytable');


Increment decimal value with active record - El Forum - 05-03-2011

[eluser]Acidrain[/eluser]
Still does not work.


Increment decimal value with active record - El Forum - 05-03-2011

[eluser]Acidrain[/eluser]
I wrote the query manually and it works.

Code:
$query = 'UPDATE table_name SET column_name=column_name+' . $myFloat . ' WHERE id_column=' . $id;
$this->db->query($query);

I guess it is active record issue.


Increment decimal value with active record - El Forum - 05-03-2011

[eluser]LuckyFella73[/eluser]
From CI Manual / active record:
Quote: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.

Code:
$this->db->set('field', 'field+1', FALSE); // that is what you need
$this->db->insert('mytable');
// gives INSERT INTO mytable (field) VALUES (field+1)

$this->db->set('field', 'field+1');
$this->db->insert('mytable');
// gives INSERT INTO mytable (field) VALUES ('field+1')



Increment decimal value with active record - El Forum - 05-03-2011

[eluser]Acidrain[/eluser]
Thank you, works. Smile