• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Views in post

#1
Hi guys, I'm looking at Codeigniter these days and I'll need your help.

I have this class that is nothing but the display of every single page, but I needed to enter a query, where every access was updated to the field "views" in the database.

PHP Code:
   public function get_post($slug FALSE) {
 
       if ($slug === FALSE) {
 
           $query $this->db->get('post');
 
           return $query->result_array();
 
       }
 
       $query $this->db->get_where('post', array('slug' => $slug));
 
       return $query->row_array();
 
   

I've added a query that add +1 to the view field, but obviously does not count. So I need to +1 the current value of the field views.

PHP Code:
   public function get_post($slug FALSE) {
 
       if ($slug === FALSE) {
 
           $query $this->db->get('post');
 
           return $query->result_array();
 
       }
 
       $query $this->db->get_where('post', array('slug' => $slug));

 
       $this->views = +1;
 
       $this->db->update('post'$this, array('slug' => $slug));

 
       return $query->row_array();
 
   
My question is, how can I get the current value of the field views from the database so that the variable is moved to +1? I hope I'm well explained, I'm using Google Translate, I'm Italian.
Thanks!
Reply

#2
you can always do this, to set your value by increment-1
PHP Code:
$this->db->set('views''views+1'FALSE)->where('slug'=>$slug); 

and this is already documented inside CI's documentation,
if you would like to explore the docs more thoroughly:

https://www.codeigniter.com/user_guide/d...ating-data
Reply

#3
Perfect thank you very much.

I solved this syntax:

PHP Code:
$this->db->set('field''field+1'FALSE);
$this->db->where('id'2);
$this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2 

Thanks!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.