My Comments Block for Profiles
Here's how we do the "My Recent Comments" feature on the Profile pages at http://bastardly.com. Note: This code assumes that you have already implemented a user_profile.tpl.php file similar to the one described here.
Add this function to your template.php file...
<?php
function phptemplate_my_comments($uid,$howmany) {
$returnstr = "<span class='field-label'>My Recent Comments:</span><br><ul>";
$i = 0;
$result = db_query('SELECT n.nid, n.title, c.cid, c.subject FROM node n INNER JOIN comments c ON n.nid = c.nid WHERE c.uid = %d AND c.status = 0 ORDER BY c.timestamp DESC LIMIT 0,%d', $uid, $howmany);
while ($r = db_fetch_object($result)) {
$i++;
$returnstr .= '<li>' . $i . ". <a href='/node/" . $r->nid . "#comment-" . $r->cid . "'>" . $r->subject . '</a>... <small><i>(on ' . $r->title . ')</i></small></li>';
}
$returnstr .= '</ul>';
return $returnstr;
}
?>and then you can call it from anywhere you want - e.g., user_profile.tpl.php. Here is how to call up the profile person's last 5 comments...
<?php
$commentblock = phptemplate_my_comments($user->uid,5);
echo $commentblock;
?>Change "5" to the number of comments you want.
Update for D6:
<?php
$commentblock = phptemplate_my_comments($account->uid,5);
echo $commentblock;
?>Just change $user to $account for profile pages. Otherwise, it always shows comments by the logged in user viewing the page instead of the user the page is about. -FJGamer
Alternatives at a cost
Note: It is also possible to put the function into a block, and assign that block to the user/* page. Just grab arg(1) and use it instead of $uid. In fact, we tried it that way, and it worked. However it caused a greater performance hit on the system, so we reverted to doing it in the template.
Or ...
<?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;
?>
What do you mean by
What do you mean by: Just grab arg(1) and use it instead of $uid.
??
in the template.php or in the user.tpl.php?
and where and how?
Many thanks
TR
by arg(1) is it meant in
by arg(1) is it meant in relation to the URL so www.example.com/users/11 would be set up so arg(0) would be where 'user' is and arg(1) would be the user ID, in this case 11. And also for this example put it in your user_profile.tpl.php file
Thanks, you mean this?
<?php$commentblock = phptemplate_my_comments($arg(1)->uid,5);
echo $commentblock;
?>
No... use the arg's within a
No... use the arg's within a conditional statement...
<?php$commentblock = phptemplate_my_comments($user->uid,5);
if (arg(0) == 'user' && arg(1) != 1) {
print $commentblock;
}
?>
The above example when used in the user_profile.tpl file would print whatever content you had within the conditional on all user profile pages except the admin super users profile page.
If you want to use the example given for the user_profile.tpl file then there is no real need for the conditional unless you want to hide or show that information to only a certain user or user group. also if you choose to use the first example just put the first snippet into your template file and the part below goes into your user_profile.tpl
Also if you want only the profile owner to see their latest comments you can do something like this
<?php$commentblock = phptemplate_my_comments($user->uid,5);
if ($GLOBALS['user']->uid == $user->uid) {
print $commentblock;
}
?>
Hope that clears things up for you.
Thanks but sorry
The first snippet you provide works in a block but is not user specific. It just returns all recent comments, not those by that specific user (something similar already comes with the Views module).
Your second snippet give the latest comments from user X (as I want) but when I put it in a block nothing happens, nothing shows up.
What I wanted is: that user X has a block with only his own latest comments.
Thanks
TR
FYI: I have checked: this cannot be done in Views for D5
The examples I gave are to
The examples I gave are to be used in your user_profile.tpl file. Not in a block.
Well its unfortunate that you wasted my time then
because it was clear from the beginning that I was referring to a block.
No need to be rude. just
No need to be rude. If you kindly look at your first post, you'll notice you made no mention to using a block. all you mentioned was the template and user_profile files. I'll assume your new here, and I will ignore the above comments. Or shall I offer to refund the money you paid me?
Just have a look around the forums and you'll find plenty of examples on how to set up a view that is user specific.
The first occurrence in the
The first occurrence in the regular text of the word 'block' is in the same paragraph as the 'arg-'related- text so why you didnt understand that I was talking about a block solution escapes me.
I am not a newbie, I have been using Drupal for at least two years and though I do really see its strong points, and have sponsored the development of an e-commerce module and donated to support the development of some others I have also had many discouraging experiences. I have enough community spirit to want to give a positive contribution here, but that can only be realized if the management of my sites were made so easy by the use of Drupal that I would have spare time to teach others the gospel. Alas the opposite is the case. The management of Drupal takes so much time that I cannot find the time to do the work that makes me money ( I am a journalist). One important reason for that is the miserable way some modules are maintained, the chaos on the website and the sloppy answering of questions in the forums - if at all. Since I am an optimist and think that everything on this planet is improving, I think Drupal will too. The fun though has long gone, Drupal is my prison.
Thanks for the invitation to visit the forums. I know my way there and have found several locations where the above problem is brought up and always with the conclusion that it wasnt possible, at least not in D5 (Views for D6 apparently handles it). I knew that and got the impression from you that you really had a solution. I was then disappointed again and agree that I was rude - though not wrong.
Any "fix" to make this a block that will work anywhere?
I'm looking for this solution as well, and it seems to be so close. I have the code setup in the template and am adding a block to display - but I want to show the block everywhere on the site, not just the user-profile page. Is this possible to do? At the moment, it is returning comments, just not specific to the user logged in.