CodeIgniter Forums
Hit Counter for items - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Hit Counter for items (/showthread.php?tid=61121)



Hit Counter for items - El Forum - 09-22-2014

[eluser]jjimenezweb[/eluser]
Hi,

I am new to Codeigniter, and not very well as it is all about.

I would like to make a hit count for a few items that I have on my site.

If someone has already done or can give me some guidelines to do so would appreciate a lot.

View + Controller + Models

Regards,


Hit Counter for items - El Forum - 09-22-2014

[eluser]CroNiX[/eluser]
This would be pretty simple. On the pages you want "counted", just have an update query in the controller that loads that page that just increments a "views" column in the db table. It's just a simple query, that could easily be placed in a model.

The query would just be something like:
Code:
$this->db->where('page_id', $page_id); //however you are storing your page identifier
$this->db->set('page_views', '`page_views`+ 1', FALSE); //increment counter where page_views is an INT with default value of 0.

Another solution is to just use google analytics, which automatically does this and tons more.


Hit Counter for items - El Forum - 09-22-2014

[eluser]jjimenezweb[/eluser]
To implement this, I have to create a new function or I can put it in any function already?


Hit Counter for items - El Forum - 09-22-2014

[eluser]CroNiX[/eluser]
However you want. If you're going to be using it in a lot of places, I'd make a library or helper so you can just reuse the code instead of copying it all over the place.


Hit Counter for items - El Forum - 09-22-2014

[eluser]jjimenezweb[/eluser]
So I have my code in the model.

Code:
public function set_hits($id){
$this->db->set('property_hits', '`property_hits`+ 1', FALSE);
$this->db->where('property_id', $id);
$this->db->update('property');

}