Community Documentation

Display (x) List of The Latest Comments using DIV

Last updated January 8, 2007. Created by pwolanin on April 3, 2006.
Edited by cel4145, zealy. Log in to edit this page.

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;
?>

About this page

Drupal version
Drupal 4.7.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here