Drupal: Latest Comments Block in User Profile.
In Drupal 5, if you want the User Profile page (e.g. http://example.com/user/stevejobs) to display latest comments by that user, you can apply a quick hack:
- Create or edit user.profile.tpl.php file in your theme
- Add the following code anywhere (wherever you want latest comments to appear) in the file:
<?php
$output = ""; $nlimit = 7;
$userid=$user->uid;
// ATTENTION: status=0 - approved, status=1 in queue.
$query= "SELECT c.cid, c.nid, c.name, c.subject
FROM {comments} c WHERE c.uid = %d AND c.status = 0
ORDER BY c.timestamp DESC";
$result = db_query_range($query,$userid,0,$nlimit);
$output .= "<div class=\"item-list\"><ul>\n";
$no_comments = mysql_affected_rows ();
if ( $no_comments > 0 ) {
while ($obj = db_fetch_object($result)) {
$link = url("node/$obj->nid");
$link = $link."#comment-".$obj->cid;
$output .= "<li><a href=\"$link\">$obj->subject</a></li>";
}
} else {
$output .= 'No Comments left so far.';
}
$output .= "</ul></div>";
print $output;
?>

thank you sir
thanks!
Drupal: Latest Comments Block (Profile)
Hi,
thx a lot for this snippet. Just one thing: You've forgotten to close the <li> Tags, so line
$output .= "<li><a href=\"$link\">$obj->subject</a>";should be
$output .= "<li><a href=\"$link\">$obj->subject</a><li>";XHTML vs HTML
Wayne,
<li> tags only need to be closed in XHTML, HTML does not care. So, the original code will still work in most browsers, but you are right, it is not quite clean.
Thanks for the correction, I am going to update the post.