Display latest 5 Guestbook entries

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;
}
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.