PHP - Page Counter

Discussion in 'PHP' started by cancer10, Aug 9, 2008.

  1. #1
    Hi

    I am trying to create a page counter in PHP.

    Table structure:
    
    Table Name: tbl_counter
    Column Name:total_views
    Column Datatype: integer
    Code (markup):
    I am using the following SQL query on the page.

    
    UPDATE tbl_counter SET total_views=total_views+1
    Code (markup):
    I know its weird but, sometimes the counter increases twice instead of 1, any idea why?
     
    cancer10, Aug 9, 2008 IP
  2. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #2
    I looks like th request is sent twice
     
    nabil_kadimi, Aug 10, 2008 IP
  3. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How come?

    I refresh the page only once.
     
    cancer10, Aug 10, 2008 IP
  4. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #4
    please post your php code
     
    php-lover, Aug 10, 2008 IP
  5. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #5
    it can only be when the counters is started twice, as the code only ups one.
     
    EricBruggema, Aug 10, 2008 IP
  6. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #6
    You should post more code

    Thanks
     
    nabil_kadimi, Aug 10, 2008 IP
  7. deleted-account

    deleted-account Active Member

    Messages:
    655
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    85
    #7
    Should be like:

    while($rows=mysql_fetch_array($connectiontoyourdatabase))
    {
    $rows['pageviews']++
    }

    Not sure if that will work but give it a try anyways
     
    deleted-account, Aug 10, 2008 IP
  8. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    k. Thanx for the reply.
     
    cancer10, Aug 16, 2008 IP
  9. oxidati0n

    oxidati0n Peon

    Messages:
    744
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Lol above that's the worse possible type of script you can get.

    do this.

    //MySQL
    
    /*The MySQL query to the updater.*/
    CREATE TABLE hitlogs ( hits BIGINT( 40 ) NOT NULL );
    
    Code (markup):
    PHP code
    
    <?php
    //Connect to the DB
    $connection = mysql_connect("localhost", "user", "pass");
    mysql_select_db("database", $connection);
    
    //The function to program it.
    function update_hits($con)
    {
    if(!mysql_ping($con)) { return false; } else {
    mysql_query("UPDATE IF EXISTS hitlogs SET hits=hits+1 LIMIT 1;",$con);
    if(!mysql_affected_rows($con))
    {
    mysql_query("INSERT INTO hitslogs (hits) VALUES (1);");
    }
    return true;
    }
    }
    
    
    //Update the hits
    update_hits($connection);
    ?>
    
    Code (markup):
    That's probably the most optimized piece of code you can possibly make, You should look at hit counters - they handle all of the usage for you. You could additionally use files and fwrite with (int) to log it, It's less heavy on MySQL.
     
    oxidati0n, Aug 16, 2008 IP