Welcome Guest, Not a member yet? Register   Sign In
form, update existing value with 1
#1

[eluser]Unknown[/eluser]
In my model, I want to update the existing value (int) with +1 everytime the form is submitted. Can someone please help me how to write the query with active records? Thanks.

/Dan
#2

[eluser]Cristian Gilè[/eluser]
model:

Code:
private function _get_value($id)
{
   $this->db->select('field_name'); //field name for int value
   $this->db->where('id',$id);
   return (int)$this->db->get('table_name')->row()->field_name;
}

public function set_value($id)
{
   $new_value = $this->_get_value($id) + 1; //increment by 1

   $this->db->update('table_name', array('field_name' => $new_value), array('id' => $id))
}

In the controller call the set_value method.

Cristian Gilè
#3

[eluser]Roy MJ[/eluser]
Making a primary key would be the easiest way.And then set it as auto_increment.

http://www.w3schools.com/sql/sql_primarykey.asp




Theme © iAndrew 2016 - Forum software by © MyBB