Popular Blog Entries: HowTo
Well I decided to keep track of the top 10 popular blog entries by pageview on my site. In order to do that I needed to modify my blog table so that it could keep track of pageviews.
Now I only keep track of pageviews for visits to the actual entry itself. How do I do that?
It’s fairly straight-forward actually, all I needed to do is add a couple of lines to my entry.php page so that it finds out how many views it already has and then increment it by one.
// increment PageView tracker$pageViews = $entry['views'];$pageViews = $pageViews + 1;$strqry = "UPDATE myTable SET views = '{$pageViews}' WHERE id = '{$entry['id']}'";$query = mysql_query($strqry) or die("MySQL Error: <br />{$strqry} <br />". mysql_error());
So as you can see it really isn’t all that difficult, I add the value of the previous views $entry[‘views’] to $pageView so that I can then increment $pageView by 1. I then just do a simple UPDATE query to reflect the new value.
Over on the left column of this site I do something very easy to display the top 10 most visited entries.
$strqry = "SELECT * FROM myTable WHERE views > 0 ORDER BY views DESC LIMIT 10";
As you can see it select everything from myTable where views are greater than 0, order it by the amount of views descending and then limit the output to the top 10.
Edited:One thing that I should mention that I forgot to was that the above code snippets are for PHP and were used in my custom blog script.






Hi Tyler,
I can’t find an email address or contact form for you here. Please consider having a way for your blog readers to contact you easily. Please send me your mailing address so we can send you your seagate contest prize.
Thanks!
Tyler, while I can appreciate the fun of doing the site on your own, I think I would pull my hair out eventually doing my own version of a WP type site. As much as I enjoy coding at home, I usually get my fill at work and like to be lazy with WP at home.
Thanks for the good tip!