Page 1 of 1

simple php guestbook----with bug

Posted: Fri Mar 12, 2004 6:08 pm
by horesh_a
hi, im new to PHP and treid to create a simple guestbook
i think i got it right but it apears that every NEW entery
erase all the previous ones and shows only her.

yes, i CAN chane the r+ in line 32 to a+
but i want the latest messages to be displayed first

hope someone can help
with respect
adam

p.s. please ignore the "wired" signing on the buttons, its just hebrew :)


<html><head><title>forum</title></head>
<body background=home_back.jpg>
<form method="get" action="">
<b>
:םש<br>
<input type="text" name="name" size="8"><br>
:העדוה</b><br>
<textarea name="comments" cols=30 rows=5></textarea><br><br>
<input type="submit" value="שלח" name="submit" style=
"width:50;text-align:center;cursor:hand">
</form><br>

<?php

if(!empty($name)&&!empty($comments))
{
$data =strrev($comments)." &nbsp; :<b>".strrev($name)."</b>".
"<img src=rec_purple.gif><br>";
$file = fopen("0forum.txt","r+");
fwrite($file,$data);
fclose($file);
}

//display all messages
$file=fopen("0forum.txt","r+");
fpassthru($file);
?>
</body></html>

Posted: Fri Mar 12, 2004 6:20 pm
by Illusionist
the way i did long ago when i didn't use mysql, was jsut use append. Then when i opened the file, i read each new line into an array, then displayed it backwards, so it would show the most recent first!! I may be able to find some old code for an example... ill look

Posted: Fri Mar 12, 2004 6:24 pm
by Illusionist
actually the way i did it was open the text file, read everything in it. close it. then opened it again with "w" and wrote the new string, then looped through the old stuff writing it in...

Code: Select all

$fn = "tags.txt";
$ff = fopen($fn, "r");
$previous = file($fn);
fclose($ff);

$ff = fopen($fn, "w");
fwrite($ff, "new string<hr>\r\n");
for ($n = 0; $n < count($previous);$n++) {
	fwrite($ff, $previous[$n]);
}
fclose($ff);
You may not even need to loop back through the old text, you may be able to jsut write it too, but i haven't tested that!

Posted: Sat Mar 13, 2004 4:26 am
by horesh_a
1. thanks very much :)

2. i fought doing just "r+" should do it but it doesn't...

3. puting lines in an array is a good idea, i should use it
so viewer can post a reply to messages

regards from israel
adam