View Full Version : Counting number of view for news and etc...
06-29-2010, 06:00 AM
How you PHP coders keep track of number of views of an item?
For example I have a news table in my MySQL and ofcourse a field named "views".
What is the best way to store number of views in this field, and perhaps each IP counted 1 time?
06-29-2010, 06:44 AM
I'd have a Text field called IP_addresses and my View field would be an Int type. Then on each page view I'd check the IP's already in the IP_addresses field and if not found I'd inc the Views field.
In the text field I'd store the IPs like this:
and then all you need to do is explode() the data by comma and you'll have them all in a neat array to loop through for checking.
06-29-2010, 07:08 AM
Thanks, but using this approach, wouldn't be the size of file a problem in a long run?
Can you also show some codes too please :D?
06-29-2010, 07:42 AM
The text field solution would easily become huge and unmanageable. Eventually, exploding and iterating would be a very slow proposition.
You're best bet is to create a table for views with 3 columns: IP, timestamp, and thing viewed.
Then, in a common include, you can have an insert run with IP, timestamp, and script name.
On the table, you can create an insert trigger to determine what to do with things. If you only want to track unique views within a 24-hour period, your trigger should select for the IP and script name within 24 hours and only insert if it find no records, and ignore the insert other wise.
When you do it this way, you'll be able to run cron scripts that will clean up this tracking table on a regular basis, so you only keep a month of records, or a rolling month of records. You can also aggregate your views by timeframe with this method.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.