Welcome Guest, Not a member yet? Register   Sign In
Increase for db->update possible?
#1

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

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

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB