Looks good so far, if I understand you correctly you're trying to write a php guestbook using a flat file. I recommend instead of having the "ShowGuestbook.php" file writing in the markup for each of the posts, have the WriteGuestbook part write it into the flat file with html tags already there. That way you can change your current writing out guestbook.txt line by line with <br>'s to something simple like:
Code:
<?php
//Title
print "<center><h1>[TITLE]</h1></center>\n";
include('guestbook.txt');
?>
if you wanted the title to be easily specified by the user you can store it in a config file and require that config at the beginning of the php and use the variable from it.
moving onto the writing to the guestbook part, I'm not sure what your form looks like, but I'll just proceed assuming it's only has two values being POSTed, a name and a post. So now we begin the write to guestbook file code:
Code:
<?php
//open the guestbook to write
$fileBase = fopen ("guestbook.txt","a");
fputs ($fileBase, "<div class=\"name\">Posted by: " . $name . "</div><div class=\"post\">" . $post . "</div>");
fclose ($fileBase);
?>
or if you want the new posts to be at the top
Code:
<?php
$fp = fopen ("guestbook.txt", "rb");
$pastentries = fread ($fp, filesize ("guestbook.txt"));
$fclose ($fp);
//open the guestbook to write
$fileBase = fopen ("guestbook.txt","a");
fputs ($fileBase, "<div class=\"name\">Posted by: " . $name . "</div><div class=\"post\">" . $post . "</div>" . $pastentries);
fclose ($fileBase);
?>
The $name and $post variables should come from your form's POST data. From here you can style the name and post classes however you want in a css file. Writing to the top of the file, I first read in the entire file to a string, then write the post and append the entirefile string to the end. Hope this helps to get you started.
Last edited by SeventotheSeven; 10-30-2007 at 04:07 PM..
|