Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Following PHP snippet can be used in a block to display latest 5 Guestbook entry teasers for the logged in user. This block can be used only if Guestbook module is installed & activated.
I have tested & used this snippet on Drupal 5.1 only. I am a non-programmer, so please use it at your own risk!
Any suggestions, enhancements or modifications to this code are definitely welcome!
<?php
global $user;
$uid=$user->uid;
//define various messages text for use in the block
//to be displayed at the top of the guestbook entries
$gb_top_message='Latest entries from your guestbook!<br/>';
//to be displayed at the bottom of guestbook entries , I have given a link to the user's guestbook
$gb_bottom_message='<div align=right>' . l('View Full Guestbook', "guestbook/$user->uid", array('title' => 'View Guestbook')) . '</div>' ;
//to be displayed when there are no entries in the guestbook
$gb_nomessages_message='Your guetbook does not have any entries<br/>';
//to be displayed if user guestbook is disabled
$gb_nogb_message='Your guestbook is Disabled, to enable,'. l('click here',"user/$user->uid/edit", array('title' => 'Edit Profile')) .' and change settings';
if (module_exists('guestbook')) {
if(!$user->guestbook_status){ // guestbook_status seems to store 0 for enabled and 1 for disabled
$result = db_query(
"SELECT left(g.message, 50) as message, g.created, u1.name as author
FROM {guestbook} g
LEFT JOIN {users} u1 ON g.author = u1.uid
WHERE g.recipient = %d
ORDER BY g.created DESC
LIMIT 5",
$uid);
//check if there are entries in the GB
if (db_num_rows($result)){
//if yes, print top message
print $gb_top_message;
while ($entry = db_fetch_object($result))
{
//print GB entries
print '<b>'. $entry->author .'</b> : '.strip_tags($entry->message) .'<br>';
}
}
//if no messages in GB, print message
else{ print $gb_nomessages_message;
}
//print bottom message
print $gb_bottom_message;
}
else{
//if GB not enabled, print message
print $gb_nogb_message;
}
}
?>
Comments
How to display generally latest 5 guestbook entries
Hallo,
Rather then displaying guestbook entries for the logged in user, how can I display the latest 5 entries in the guestbook? regardless of logged in user?
Simplicity is the ultimate sophistication, Leonardo da Vinci
Try this
If you havent already tried, try removing the WHERE clause from the $result query like this
this is an adaption for D6
this is an adaption for D6 that shows latest guestbook entries regardless of user...
Shows entries 2 to 6, not the actual Latest entry.
This works but never shows the latest guestbook entry. For example if you wish to show the latest 5 entries then it will show entries 2 to 6, but not the actual latest entry. This is a bit confusing for people entering comments as they expect their new comment to top the list and it never does (or at least not until another there is another newest entry).
To someone this is really easy to fix, sadly I am not that someone!
Not sure why, but...
I must admit I'm way too rusty to know exactly why, but commenting out the db_result lines made it work for me. Not sure if there's some PHP 101 I'm missing, or it's just my limited knowledge of Drupal and blocks, but it worked :)
A bit of a hack, and would give you trouble if the result is empty, I think. But maybe it'll help you - or someone else -work out a better solution :)
Improve author handling
if you change the sql query to this, you will have the user's name if a registered user has posted it.
when printing the name, I use this code:
not sure that strip_tags is needed there, but I put it there anyways
Perfect, thanks - I was
Perfect, thanks - I was wondering how to fix that, but got away from it.
User Profile context
How do I show the guestbook block in the context of the user profile of that page?