You are hereHome / Blogging / Drupal Most Popular Posts Block

Drupal Most Popular Posts Block


By Gerd Riesselmann - Posted on 02 December 2005

I'm currently experimenting if and how presenting users links to other content of this site affects the site navigation (and how you may have guessed: Yes, I got myself a Google Analytics account, too). First I decided to display related articles beneath each articles. Now I'm trying a "Most popular" block that displays the top five pages visited within the last three days. Here's how I did it.

First, the Drupal statistics module must be enabled, since it will gather all the data for you. Second, I created a custom block and placed the following code within:

<?php
$sql
= "SELECT COUNT(path) AS hits, path, title FROM {accesslog} GROUP BY path, title having path like 'node/%' and path not like 'node/feed%' order by hits desc limit 5";
$rec = db_query($sql);
$list = array();
while (
$obj = db_fetch_object($rec)) {
 
$list[] = l($obj->title, $obj->path, array(), NULL, NULL, FALSE, TRUE);
}
print
theme("item_list", $list);
?>

That's all. Dude, I like Drupal's block mechanisms. It's just so easy to add stuff to the navigation area.

Update: Added "and path not like 'node/feed%'" to the SQL to exclude Feeds.

Hi Gerd!

You got a Google Analytics account? Wow... Did you already get it working? I'm waiting for ages now, and still nothing's going on there...

About this block of you: seems to be very interesting! I won't implement this straight away (as it's 4 o'clock at night and I have an appointment early tomorrow morning :-\ ), but it's certainly something keep in my mind!

Thanks for the info, thanks for the code!

BTW
Do you already have experience with coding modules?

Have a nice weekend!
Marc

Hello Marc,

I got myself an account right away the first day Analytics went online. Except the delay everybody faced, things went well. However, I added a second site to monitor on Friday evening and it still isn't even recognized as valid. I have no idea, what's happening there, but filed a support request for Google. Maybe they can figure things out.

And yes, I did some modules, namely the TotalFeeds module and a bunch of others I needed for the newspaper site I did. I did not request a Drupal CVS account yet, though.

Hi Gerd,

Thanks for the block! Strange tho because I tried creating it and nothing gets displayed for it. I have the statistics module enabled, and I'm using drupal 4.7. Any ideas?
briansevolution.com
Thanks!
Brian

Hmm... didn't work for me either.

Topics