Welcome Guest, Not a member yet? Register   Sign In
Increment decimal value with active record
#1

[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)
#2

[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');
#3

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

[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.
#5

[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')
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB