3

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
alan
  • 391
  • 1
  • 4
  • 8

3 Answers3

3

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.

Koray Al
  • 322
  • 3
  • 10
1

You can load all comments for a node in drupal 7 using the function:

comment_get_thread($node, $mode, $comment_per_page)

Check out the documentation here: http://api.drupal.org/api/drupal/modules%21comment%21comment.module/function/comment_get_thread/7

It also discusses the default sort parameter. This does not however give you an easy way to resort the comments. I would just use views for that. Then you can use hook_node_view to disable the default comment display and add views_embed_view('my_view', 'my_display');

danielson317
  • 3,121
  • 3
  • 28
  • 43
0

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.