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 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2007
    Location
    Norway
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Count article view

    I'm about to make a php CMS system where I can add articles through a web interface. The articles are stored in a MySQL database. I plan to implement a feature that counts the views of the articles. I guess this can be done so that the views count is stored in a field in the database table.

    Can anyone suggest a method to count the number of views for each specific article? Such a field in the MySQL table would be handy, because it can be used to filter show lists of the most popular articles and so on.

    Wombat

  • #2
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    what you would do is pull out a row called views when you pull out the content then simply update the row to $views++ everytime its viewed its storing how many times it's been viewed but when you echo "Article seen $views"; make sure you have already ++'d it

  • #3
    New Coder
    Join Date
    Feb 2007
    Location
    Norway
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you. I think something like you describe is the way to go. I found this tutorial which explains exactly what I need: http://www.phpfreaks.com/tutorials/6/0.php

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Actually, that way would be silly. Why download the content and then send it back? If you can avoid roundtrips with SQL, then do.

    Code:
    UPDATE articles SET views = views + 1 WHERE id = ?
    This would be on the page that displays the article.

  • #5
    New Coder
    Join Date
    Feb 2007
    Location
    Norway
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I use this piece of code to store the views in my database. It is placed on top of each document, before the html start tag:

    PHP Code:

    if ( isset($_GET['id']) ) 

    {
        
    $condition "WHERE articleid = '" $_GET['id'] . "'";


    $connection ConnectTo("MyDatabase");
    session_start();

    if (!
    session_is_registered("counted"))

    {
        
    mysql_query("UPDATE articles SET views=(views + 1) WHERE articleid = '" $_GET['id'] . "'");
        
    session_register("counted");

    This piece of code works perfect on the first page the user clicks on, but if the user clicks on another article this is not counted. Is there some way to get around this problem, so that each article is counted?

  • #6
    New Coder
    Join Date
    Feb 2007
    Location
    Norway
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No one that know of a way to get around this problem?

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    It's only counting the first page because the session variable persists across all pages viewed while the session is active. So make an array of articles viewed and just check to see if the article id is in it.
    PHP Code:
    if(!is_array($_SESSION['viewed_articles']) || !in_array($_GET['id'], $_SESSION['viewed_articles']))
    {
        
    // increment view counter
        
    $_SESSION['viewed_articles'][] = $_GET['id'];

    Not tested.


  •  

    Posting Permissions

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