Click to See Complete Forum and Search --> : PHP newbie needing some help....


mentaleruptions
06-30-2003, 02:05 AM
Hey all im new to PHP and im needing some help solving a problem.
Im using a script for a guestbook thats PHP and when a new entry is posted the newest entry goes to the bottom of the list instead of the top the way u would think it would.
Heres the PHP script.


<?

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=\"#990000\"><u><A href=\"mailto:$email\">$email</A></u></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@youremail.com";
$mailFrom = "From: <you@youremail.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);
}

print "&result=okay";
exit;

?>


Now i really cant work out what part of the script controls the order the posts are viewed. Could someone help me work this out ?? Im sure its something simple that im missing here.

Thanks in advance, Mental

pyro
06-30-2003, 08:40 AM
Using a+ will append it to the end of the file, so perhaps the code that displays the guestbook is reversing the entries?

mentaleruptions
06-30-2003, 10:43 AM
Hi pyro im not sure :).
Its a Flash MX guestbook and im still learning flash also.
Do u use flash ??
I could attach the files if u think u could help me.
I would just email the person that sent it me but i dont want
to bug them if i can get help on here :D

Thanks again, Mental

pyro
06-30-2003, 10:51 AM
Sorry. I only use Flash on a beginner level. Not sure how it handles file imports, so that would make it hard to check why it is displaying newer data at the bottom rather than the top, rather than the fact that displaying new entrys on the bottom would be the default...

brendandonhue
06-30-2003, 01:44 PM
change
$file = fopen('entry.txt', 'a+');
to
$file = fopen('entry.txt', 'r+');
But make sure the file entry.txt exists on the server, because that will not create the file if it doesn't.

pyro
06-30-2003, 02:02 PM
True, that would be one way of doing it, as r+ sets the file pointer to the beginning of the file...

mentaleruptions
06-30-2003, 03:45 PM
Ok now that change does work but it the causes another problem.
For the text file with the entries to be read by Flash it needs to have:

&result=okay&entries=

At the start of it and all entries after that.
BUT when i change that bit of code it adds the new entries latest to the top but it wipes that bit of code and also any other entries in the text file so any ideas ??

Thanks once again, Mental

brendandonhue
06-30-2003, 04:10 PM
Your going to have to change the way the flash file handles things probably.