Show a list of forums which contain the most popular forum posts

This code shows a list of the five forums which contain the most-viewed forum posts. Even if a forum contains two or more of the most popular posts it is still displayed just once.

Thanks to kbahey for this code.

<?php
$num_nodes
= 5;

$sql = "
SELECT DISTINCT(tn.tid), td.name
FROM {node} n
LEFT JOIN {term_node} tn ON
n.nid = tn.nid
LEFT JOIN {term_data} td ON
tn.tid = td.tid
LEFT JOIN {node_counter} nc ON
n.nid = nc.nid
WHERE n.type = 'forum'
ORDER BY nc.totalcount ASC"
;

$result = db_query_range(db_rewrite_sql($sql), 0, $num_nodes);
while(
$row = db_fetch_object($result)) {
$list[] = l($row->name, "forum/$row->tid");
}

print
theme('item_list', $list);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.