Drupal Most Popular Posts Block
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.
Comment by Marc
December 10, 2005 - 04:02
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