Display (x) List of The Latest Comments using DIV

Last modified: January 8, 2007 - 21:44

moderator's note: this code was not tagged with a Drupal version, I'm guessing it's for 4.7

<?php
function _get_latest_comments($count = 10) {
 
$sql = 'SELECT c.nid, c.subject AS subject, c.cid, c.timestamp, n.title AS title FROM {comments} c LEFT JOIN {node} n ON n.nid = c.nid WHERE c.status = 0 ORDER BY c.timestamp DESC';
 
$sql = db_rewrite_sql($sql, 'c');
 
$result = db_query_range($sql, 0, $count);
  return
$result;
}

   
$result = _get_latest_comments();
   
$items = array();
    while (
$comment = db_fetch_object($result)) {
      
$time preg_replace("/ /","",format_interval(time() - $comment->timestamp));
      
$items[] = l($comment->subject, 'node/'. $comment->nid,
                         array(
'title'=>$comment->title),
                        
NULL, 'comment-'. $comment->cid) .' '. t('%time ago', array('%time' => $time));
    }

 
$output = "<div class=\"latest-comment-list\">";

  if (!empty(
$items)) {
   
$output .= '<ul>';
    foreach (
$items as $item) {
     
$output .= '<li>'. $item .'</li>';
    }
   
$output .= '</ul>';
  }

 
$output .= '</div>';
  print
$output;
?>

 
 

Drupal is a registered trademark of Dries Buytaert.