Page 1 of 1

view counter code

Posted: Fri May 08, 2009 4:59 am
by irshad_sheikh
Hello,

Am I on the right track to create code for view counter in php?
I am using codeIgniter framework.
My code is:

Code: Select all

 
function update_view_count($topic_id)
         {
            $count = 0;
                $query = "SELECT viewcount FROM forum_topics WHERE id = '".$topic_id."'";
                $temp=$this->db->query($query);
                foreach($temp->result() as $value)
                  {
                      $count = $value->viewcount;
                  }
                $count = $count + 1;
 
                $query="UPDATE forum_topics SET viewcount = $count WHERE id=$topic_id";
                $temp = $this->db->query($query);
                return $count;
        }
 
or is there any other way?

Re: view counter code

Posted: Fri May 08, 2009 5:08 am
by ben.artiss
Hi, MySQL can increment a hit counter for you:

Code: Select all

mysql_query("UPDATE forum_topics SET viewcount=viewcount+1 WHERE id=$topic_id")
It's also faster than letting PHP calculate the new number of hits - hope that helps.