tags:

views:

191

answers:

2

I have a node in drupal. I want to be able to input a node id and then then have it output a single comment id which was made on that node. How would I go about doing this? Thank you.

+1  A: 
$mycid = db_fetch_object(db_query('SELECT * FROM {comments} WHERE nid = %d ORDER BY RAND() LIMIT 1', $mynid));
return theme_comment_view($mycid,$mynid);

$mynid is your node id to load. This code will take your node id and render a random comment from that node's comments.

Check here.

hecatomber
Thanks, that is the concept I am looking for. I can't seem to get it to render the comment though. Do I need to do something instead of comment_render or some other special thing? Thanks again.
Sorry, I should be using theme_ functions. I edited the code. It should work now.
hecatomber
I am on drupal 5.x, which doesn't seem to support having $node as an argument in theme_comment_view. Is there a way to just have the function get the $cid from the node and not all the text of the comment? I think that would be easier for what I am doing.
On drupal 5 (http://api.drupal.org/api/function/theme_comment_view/5) you can just use theme_comment_view($mycid)
hecatomber
hmm it still doesn't seem to returning anything. If I try to output just $mycid it also gives me nothing. It should give me the number of the $cid correct?
I tested out the code above on D6 and it's working. So it should work too if you use theme_comment_view($mycid) on D5.The only variable you need to set before these two lines is $mynid (which is the nid of your node that has comments)
hecatomber
You should use these lines after drupal functions are usable. For example on a block, on a page node, theme file etc. Also if you are using it on a node or block be sure to choose "php filter"
hecatomber
Thank you, now it works beautifully.
+1  A: 

Do you want this to be its own page? On the node's page? In a block? Do you want a particular comment or just a random one? How do you want to 'input' the node id?

For most of the cases, I'd probably do this with the Views module. When you create a view, you can set the type to 'comment', set your filters/arguments to the particular node/node type and limit it to a single comment. If you want a page or a block, views can simply create them for you. If you want to display the whole comment or just parts of it, Views can take care of that. If you want to embed them in some other page, it's still relatively easy to to embed a view in another page through PHP.

Sean McSomething