views:

1565

answers:

2

I have a node in Drupal with a few comments. Is there an easyish way to get the CID of every comment within the node? Also, is there a way to sort them by various parameters, chronology, karma of the comment etc. Thank you.

+3  A: 

I suppose you should check the comment_render function.

But if you need your own sort parameter, it'd be easier to do it using sql commands;

Check: http://api.drupal.org/api/function/comment_render/6

You can first make a query listing all the cid's on whatever you need to order;

$myquery = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.signature, u.picture, u.data, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.nid
= %d ORDER BY c.uid ASC';

$myresult = db_query($myquery)

This query exists on the comment_render function. But I tried to modify it for my use.

Now we have the node id and the cids in the order we wanted.

Here is the rendering work;

while ($mycomments = mysql_fetch_row($myresult)){

foreach ($mycomment as $mycid)

comment_render($nid, $mycid)

}

I haven't tested this one, but I hope it helps.

hecatomber
A: 

Simpler solution:

$sql = "SELECT cid FROM {comments} WHERE nid=%d ORDER BY timestamp DESC";

$resource = db_query($sql, $node->nid);
while( $row = db_fetch_array( $resource ) ) {
  print comment_render( $node->nid, $row['cid'] );
}

Our initial SQL query only needs to fetch the comment ID (cid) as comment_render's second parameter will handle fetching all the additional information.

related questions