PDA

View Full Version : PHP Guestbook: change entry order



PiouPiou
November 8th, 2003, 02:09 AM
Hi,

I have a PHP guestbook intergrated into my flash movie, but when new entries are added they go to the bottom - I would like the newest entry to always be at the top. Can anyone please help?

Here is the PHP code:

<?

if (!isset($name) || !isset($email) || !isset($message) || empty($name) || empty($email) || empty($message)) {
print "&result=Fail";
print "&errorMsg=" . urlencode("Input required for all fields.");
exit;
}

$email = strtolower($email);

addentry($name, $email, $message);

function addentry($name, $email, $message) {

$posted = strftime("%D %I:%M %p");

$message = stripslashes($message);

$file = fopen('entry.txt', 'a+');

if (!$file) {
print "&result=Fail";
print "&errorMsg=" . urlencode("Could not open entry.txt file. Change CHMOD levels to 766.");
exit;
}

fputs($file, "<font color=\"#ffffff\">Name:</font> $name\n<font color=\"#ffffff\">Email:</font><font color=\"#ffffff\"><A href=\"mailto:$email\"> $email</A></font><br>\n<font color=\"#ffffff\">Posted:</font> $posted\n<font color=\"#ffffff\">Message:</font> $message\n\n");
fclose($file);

// Send admin an email when new entry occurs
mailAdmin($name, $email, $message);
}

function mailAdmin($name, $email, $message) {
$mailTo = "you@yoursite.com";
$mailFrom = "From: <you@yoursite.com>";
$mailSubject = "New Guestbook Entry";
$mailBody = "A visitor to your site has left the following information in your guestbook:\n
Name: $name
Email: $email
The visitor commented:
------------------------------
$message
------------------------------
You can view the message at:
http://www.yoursite.com";
mail($mailTo, $mailSubject, $mailBody, $mailFrom);
mail($email, "Your Subject", "Thank you message", "From: you@youremail.com");
}

print "&result=okay";
exit;

?>

eyezberg
November 9th, 2003, 11:42 AM
"$file = fopen('entry.txt', 'a+');"

you're opening the file in "append" mode, so new stuff goes to the end.
you need to have your new entry in a var, read in the whole txt file (previous entries), append to the new var, and writte the whole thing back, overwritting whatever was there before..

PiouPiou
November 9th, 2003, 05:21 PM
thanks for your help! I actually didn't write this code though - I don't know PHP I can just sort of understand it when I look at it - it is just part of the guestbook I am using, so I don't really know how to code what you suggested - would it be long and complicated or a simple statement?