Not signed in (Sign In)

Vanilla 1.1.10 is a product of Lussumo. More Information: Documentation, Community Support.

    •  
      CommentAuthormemic.a
    • CommentTimeAug 19th 2008
     
    Hi,
    How can I know the most viewed pictures on the entire gallery ?
    I would like to make a box with the most viewed pictures, but plogger don't have any stats.
    Thanks
    •  
      CommentAuthorsidtheduck
    • CommentTimeAug 19th 2008
     
    Currently this is not an option memic.a. There is a Trac ticket for it, so it will be incorporated at some point, but it may be a little while.
    •  
      CommentAuthormemic.a
    • CommentTimeAug 19th 2008
     
    Ok thanks.
    I will write some php that increment a counter in the database everytime that a picture is displayed.
    It's too simple but better than nothing ;-).
    I hope no one will refresh the same page 100 times :-)
    •  
      CommentAuthormemic.a
    • CommentTimeAug 27th 2008
     
    I added this code to picture.php to count the number of views and it's working fine.

    // connecting to database MySQL
    $connessione = mysql_connect("host","user","pass");

    //choose db
    mysql_select_db("db_name",$connessione);

    $id = (int)$_GET['id'];
    //echo $id.'<br>';

    $new_hits = 0;

    // get number of hits from db
    $result = mysql_query("SELECT hits FROM plogger_pictures WHERE id=$id" );

    if ( mysql_num_rows($result) )
    {
    $row = mysql_fetch_array($result);
    $old_hits = $row['hits'];
    //update hits
    $new_hits = $old_hits + 1;
    mysql_query("UPDATE plogger_count SET hits=$new_hits WHERE id=$id");
    }
    //close connection
    mysql_close($connessione);


    But, if someone refresh the page many times this code will increase the hits.
    How can I prevent this?
    I wanted to use sessions to see what pages an user viewed and for those pages the counter will not increase the hits, but my code didn't woked.
    I tried something like this: if( !isset($_SESSION["$id"]) ) //increase the hits
    $_SESSION["$id"] is a variable that I create the first time the counter is used

    Anyone have an idea on how use sessions to do this?

    P.S I know that It won't work if user have cookies disabled, but it's fine.
    •  
      CommentAuthorsidtheduck
    • CommentTimeAug 27th 2008 edited
     
    You should be able to do something like this:
    <?php
    $hit_id = plogger_get_picture_id();
    if (!isset($_SESSION['hit_id'][$hit_id])) {
    $new_hits = 1;
    $result = run_query("SELECT `hits` FROM `plogger_pictures` WHERE `id`=$hit_id");
    if ( mysql_num_rows($result) ) {
    $row = mysql_fetch_array($result);
    $old_hits = $row['hits'];
    //update hits
    $new_hits = $old_hits + 1;
    }
    run_query("UPDATE `plogger_count` SET `hits`=$new_hits WHERE `id`=$hit_id");
    $_SESSION['hit_id'][$hit_id] = true;
    }
    ?>


    That checks the session variable, runs the queries using the built-in functions already present in Plogger, updates the database, and then sets the session variable for the image id. You just have to make sure that it's located within the while(plogger_has_pictures()) loop and that you have a column created in your plogger_pictures database named "hits"

    p.s. $_SESSION variables are server-side and are not the same as $_COOKIE variables and will work with or without cookies enabled.
    p.p.s. I haven't tested this so hopefully it works out well for you.
    Thankful People: memic.a
    •  
      CommentAuthormemic.a
    • CommentTimeAug 27th 2008
     
    Thanks for the quick answer like always.

    But seems like plogger_get_picture_id() return false
    because I get

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

    SELECT `hits` FROM `plogger_count` WHERE `id`=
    •  
      CommentAuthormemic.a
    • CommentTimeAug 27th 2008 edited
     
    I will try now with (int)$_GET['id']
    it should work.
    •  
      CommentAuthormemic.a
    • CommentTimeAug 27th 2008
     
    OK, it's working great !
    I learned some important things that I didn't know about PHP today, thanks.

    But I dont understand why it didn' worked with $_SESSION["$id"] and it work using $_SESSION['hit_id'][$hit_id]
    •  
      CommentAuthorsidtheduck
    • CommentTimeAug 27th 2008 edited
     
    I don't know why it wasn't working. I tend to not like using numerical array keys when using the $_SESSION variable array because sometimes the array already has values for the default keys ([0], [1], [2], etc.), so I set it up with a sub-array under the array key "hit_id".

    plogger_get_picture_id() has to be called within the while(plogger_has_pictures()) loop and after the plogger_load_picture() call, so it depends on where you located this script in your picture.php file. I changed it from $_GET['id'] in case you or someone else wanted to use the script with the cruft-free URLs on (instead of the default).

    p.s. I was wrong about $_SESSION variables being set even if cookies have been disabled (but it's harder to disable session cookies than other cookies. To get around using session cookies, you would have to append the session id to the URL. My bad.