Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2010
    Location
    Enschede, Netherlands
    Posts
    34
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Counting number of view for news and etc...

    Hi,

    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?

    Thanks!

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    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:
    192.54.38.5,etc,etc,etc

    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.
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • Users who have thanked rfresh for this post:

    Saeid (06-29-2010)

  • #3
    New Coder
    Join Date
    Jun 2010
    Location
    Enschede, Netherlands
    Posts
    34
    Thanks
    18
    Thanked 0 Times in 0 Posts
    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 ?

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • Users who have thanked Beagle for this post:

    Saeid (06-30-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •