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

nyumbani_yangu’s picture

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

Prasad Shir’s picture

If you havent already tried, try removing the WHERE clause from the $result query like this

    $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 
    ORDER BY g.created DESC 
    LIMIT 5", 
     $uid);
jonodunnett’s picture

this is an adaption for D6 that shows latest guestbook entries regardless of user...


if (module_exists('guestbook')) {  // guestbook_status seems to store 0 for enabled and 1 for disabled

$result = db_query(
    "SELECT left(g.message, 50) as message, g.created, anonname as author
    FROM {guestbook} g
    ORDER BY g.created DESC
    LIMIT 5");

//check if there are entries in the GB
if (db_result($result)){

//if yes, print off list
print '<ul>';

while ($entry = db_fetch_object($result))
{
//print GB entries
print '<li>"'.strip_tags($entry->message).'..." - <strong>'. $entry->author .'</strong>';
}
print '</ul>';

}
}
jonodunnett’s picture

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!

nikolajb’s picture

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 :)

<?php


if (module_exists('guestbook')) {  // guestbook_status seems to store 0 for enabled and 1 for disabled

$result = db_query(
    "SELECT left(g.message, 50) as message, g.created, anonname as author
    FROM {guestbook} g
    ORDER BY g.created DESC
    LIMIT 5");

//check if there are entries in the GB
// if (db_result($result)){

//if yes, print off list
print '<ul>';

while ($entry = db_fetch_object($result))
{
//print GB entries
print '<li>"'.strip_tags($entry->message).'..." - <strong>'. $entry->author .'</strong>';
}
print '</ul>';

}
// }
?>
Orjan’s picture

if you change the sql query to this, you will have the user's name if a registered user has posted it.

$result = db_query(
    "SELECT left(g.message, 20) as message, g.anonname as author, u.name as name
    FROM {guestbook} g
    JOIN {users} u ON g.author = u.uid
    ORDER BY g.created DESC
    LIMIT 5");

when printing the name, I use this code:

if ($entry->author != ""){
   print strip_tags($entry->author);
} else {
   print strip_tags($entry->name);
}

not sure that strip_tags is needed there, but I put it there anyways

nikolajb’s picture

Perfect, thanks - I was wondering how to fix that, but got away from it.

seth.young23’s picture

How do I show the guestbook block in the context of the user profile of that page?