SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating no of views in forum posts

    Hey,

    I have a minor problem trying tp update the number of times a thread has been viewed.

    I have a simple update statement shown below:-

    PHP Code:
                    $update "UPDATE tbl_posts SET views = views + 1 WHERE thread_id = ".$_GET['thread_id']."";
                    
    mysql_query($update ); 
    But what is happening is instead of adding 1, it adds 2. I dont know why this is.

    I do the update on the page load so when a user arrives on this page the views are increased depending on the thread it belongs to..?

    Any ideas why it does this?

    Regards
    Billy

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about this method ?

    PHP Code:
    <?php
    session_start
    (); //start a session

    if ( isset( $_GET["thread_id"] ) AND !isset( $_SESSION["threadcounter"][$_GET["thread_id"]] ) ) {
        
    $th intval$_GET["thread_id"] );
        
    $update "UPDATE tbl_posts SET views = views + 1 WHERE thread_id = $th";
        
    mysql_query$update ) OR DIE( mysql_error() );
        
    $_SESSION["threadcounter"][$th] = true;


    ?>
    this little code will count just only one per session.

  3. #3
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    It worked the first time but when i went to the same page again it didnt update the count?

    And by the way, thinking about it, am i not updating the wrong table? I think i should be updating the threads table as its not the number of views the post has, its the thread..?

    Billy

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lets post here a bit more code from your program.

    While you don't know the reason why is your code processed twice, this code bloks it with a session variable. If you see another thread, that thread counted once.

    And this code is for track how much unique visitor watched that thread.

  5. #5
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is some of the code, i have other redirects on the page but that is at the top of the page.

    I dont think redirects will cause this problem anyway,

    PHP Code:
                    <?php    
                        session_start
    ();
                                            
    ob_start();
                
                    include(
    "../conn.php");
                
                    if ( isset( 
    $_GET["thread_id"] ) AND !isset( $_SESSION["threadcounter"][$_GET["thread_id"]] ) ) {
                    
    $th intval$_GET["thread_id"] );
                    
    $update "UPDATE tbl_posts SET views = views + 1 WHERE thread_id = $th";
                    
    mysql_query$update ) OR DIE( mysql_error() );
                    
    $_SESSION["threadcounter"][$th] = true;
                    }     
                    
                    
    ///////////////////////////////// T H R E A D /////////////////////////////////////////

                    
    $sql "SELECT * FROM tbl_posts WHERE thread_id = ".$_GET['thread_id']."";     
                                                                    
                    
    $result mysql_query($sql);
                    
    $numrows mysql_num_rows($result);
                    
                    while(
    $row mysql_fetch_assoc($result))
                    {
                                       
    // Pull out database details
                                    
    }

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The code you supplied only updates the count value if the visitor hasn't seen it before, so any visitors subsequent visits will not be counted.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think its connected with the redirections by header.

  8. #8
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So how can i make this code so that it updates every time the page is viewed?

  9. #9
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    set back your original code, and lets investigate around the header location codes.
    PHP Code:
    <?php
    session_start
    ();

    ob_start();

    include( 
    "../conn.php" );

    if ( isset( 
    $_GET["thread_id"] )  ) {
        
    $th intval$_GET["thread_id"] );
        
    $update "UPDATE tbl_posts SET views = views + 1 WHERE thread_id = $th";

        
    mysql_query$update ) OR DIE( mysql_error() );


    // /////////////////////////////// T H R E A D /////////////////////////////////////////
    $sql "SELECT * FROM tbl_posts WHERE thread_id = " intval($_GET['thread_id']) ;

    $result mysql_query$sql );
    $numrows mysql_num_rows$result );

    while ( 
    $row mysql_fetch_assoc$result ) ) {
        
    // Pull out database details
    }

  10. #10
    SitePoint Wizard billy_111's Avatar
    Join Date
    Jul 2009
    Posts
    1,683
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I changed the location of the "views" column as it made more sense to have it in the threads table. Thus changed to code to this:-

    PHP Code:
                    $update "UPDATE tbl_threads SET views = views + 1 WHERE thread_id = ".$_GET['thread_id']."";
                    
    mysql_query($update ); 
    Now i have tried putting this code before ALL the code in the <head> tag, AND have put the code after all the redirects etc..

    But both ways return the same result. It adds 2 instead of 1. The page redirects are at the top of the page linking to a login box but that should not matter.

    Can you see what the problem may be?


Bookmarks

Posting Permissions

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