Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Creating a Guest Book
Old 10-29-2007, 10:11 PM Creating a Guest Book
MGT-Evelath's Avatar
Average Talker

Posts: 21
Trades: 0
Merely a data-storage method. I'm trying to attempt at creating a guestbook using 2 files, (1 if possible). The form is easy enough to create using PhP; however I need a typical and easy method that can be used to "view" the guestbook once the information is provided.

"ShowGuestbook.php"

PHP Code:
<html>
<head>
<title>GuestBook</title>
<style type = "text/css">
body{
  background-color:darkred;
  color:white;
  font-family:'Brush Script MT', script;
  font-size:20pt
}
</style>

</head>
<body>
<?
$fp 
fopen("guestbook.txt""r");

//First Line will be the Title.
$line fgets($fp);
print 
"<center><h1>$line</h1></center>\n";

print 
"<center>\n";
//Prints the rest of the guestbook
while (!feof($fp)){
  
$line fgets($fp);
  print 
"$line <br>\n";
// end while

print "</center>\n";

fclose($fp);

?>

</body>
</html>
Just to make sure I've got things right:

This code should open the "guestbook.txt" file in a 'read-only' format which allows the user to view the different guests that have provided information. Is it viable? From here, I need to get the correct code that will allow the user to "write" to that file.

PHP Code:
<html>
<head>
<title>Write Guestbook</title>
</head>
<body>
<?
// Open the guestbook to write

//open the output file
$fileBase str_replace(" ""_"$GuestBook.txt);
Kind of stuck after this point, any help?
MGT-Evelath is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-30-2007, 04:04 PM Re: Creating a Guest Book
SeventotheSeven's Avatar
Novice Talker

Posts: 12
Name: Jeff
Trades: 0
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..
SeventotheSeven is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Creating a Guest Book
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46715 seconds with 12 queries