...

View Full Version : Formatting a txt file for a Guestbook? *help*



JayStang
01-06-2006, 10:29 PM
Hi guys, i'm new here. I have a question reguarding php. I've never programmed with php before so baer with me here. I have a php file for a guestbook which works great. It reads and writes to a .txt file. When it displays it shows everything with a white background. My question is, would it be possible to make it so every other post has a different color background to make it easier to read? Example. Post1=blue, Post2=White, Post3=Blue, Post4=White...etc. I was thinking maybe an if statement that detects if its a even or odd number post and assign a background color to it. but again i've never programmed in PHP so i have no idea how to go about doing this. Below is a copy of the PHP code that i'm using. Any help would be great.



<?php

// If you are using an old version of php, remove the next set of lines.
// or use $HTTP_POST_VARS["..."] instead.
$Submit = $_POST["Submit"];
$Name = $_POST["Name"];
$Email = $_POST["Email"];
$Website = $_POST["Website"];
$Comments = $_POST["Comments"];
$NumLow = $_REQUEST["NumLow"];
$NumHigh = $_REQUEST["NumHigh"];

// Replace special characters - you can remove the next 5 lines if wanted.
$Name = ereg_replace("[^A-Za-z0-9 ]", "", $Name);
$Comments = ereg_replace("[^A-Za-z0-9 \@\.\/\']", "", $Comments);
$Website = eregi_replace("http://", "", $Website);
$Website = ereg_replace("[^A-Za-z0-9 \@\.\/\'\~\:]", "", $Website);

// Remove slashes.
$Name = stripslashes($Name);
$Email = stripslashes($Email);
$Website = stripslashes($Website);
$Comments = stripslashes($Comments);

// ###################################################################################
// ########## Reading and Writing the new data to the GuestBook Database #############

if ($Submit == "Yes") {
// Next line tells the script which Text file to open.
$filename = "GuestBook.txt";

// Opens up the file declared above for reading

$fp = fopen( $filename,"r");
$OldData = fread($fp, 80000);
fclose( $fp );

// Gets the current Date of when the entry was submitted
$Today = (date ("l dS of F Y"));

// Puts the recently added data into html format that can be read into the Flash Movie.
// You can change this up and add additional html formating to this area. For a complete listing of all html tags
// you can use in flash - visit: www.macromedia.com/support/flash/ts/documents/htmltext.htm

$Input = "Name: <b>$Name</b>
Email: <b><u><a href=\"mailto:$Email\">$Email</a></u></b>
Website: <b><u><a href=\"http://$Website\" target=\"_blank\">$Website</a></u></b>
Comments: <b>$Comments</b><br>
<i><font size=\"-1\">Date: $Today</font>

.:::.";

/* This Line adds the '&GuestBook=' part to the front of the data that is stored in the text file. This is important because without this the Flash movie would not be able to assign the variable 'GuestBook' to the value that is located in this text file */

$New = "$Input$OldData";

// Opens and writes the file.

$fp = fopen( $filename,"w");
fwrite($fp, $New, 800000);
fclose( $fp );
echo '&load=loaded&';
}

// ###################################################################################
// ######### Formatting and Printing the Data from the Guestbook to the Flash Movie ##



// Next line tells the script which Text file to open.
$filename = "GuestBook.txt";

// Opens up the file declared above for reading

$fp = fopen( $filename,"r");
$Data = fread($fp, 800000);
fclose( $fp );

// Splits the Old data into an array anytime it finds the pattern .:::.
$DataArray = split (".:::.", $Data);

// Counts the Number of entries in the GuestBook
$NumEntries = count($DataArray) - 1;

print "&load=loaded&&TotalEntries=$NumEntries&NumLow=$NumLow&NumHigh=$NumHigh&GuestBook=";
for ($n = $NumLow; $n < $NumHigh; $n++) {
print $DataArray[$n];
if (!$DataArray[$n]) {
Print "<br><br><br><b>No More entries</b>";

exit;
}
}

?>

Thanks,
~Jamie

Element
01-06-2006, 11:20 PM
using the explode() function can help. Format your input like:



$content_for_text_file = $Name . "|" . $Email . "|" . $Website . "|" . $Comments . "\n";


And then insert that insert that, then to retrieve the data to output on the guest book, you would read the lines, and for each line you would explode it into an array:



$CommentData = explode("|", $line);

foreach($CommentData as $value) { // This is a test, and should output the values from the line.
echo $value . "<br />\n";
}

JayStang
01-06-2006, 11:26 PM
thank you for the respoce. at the risk of sounding stupid here... i have absolutly no idea what that code does. lol. this is really my first time even looking at php. could you explain what exactly it does? and how i use it?

Thanks again,
~Jamie

jskaar
01-06-2006, 11:32 PM
I think the easiest way would be something like this:


$counter = 0;
for ($n = $NumLow; $n < $NumHigh; $n++)
{
$style = "";
// Every other inlay will be formatted with a red background.
if ($counter % 2)
{
$style = " style='background-color:red;' ";
}

// Perhaps you should do the outputting to a table instead?
print "<p " . $style . ">" . $DataArray[$n] . "</p>";

$counter++;
}

If this doesn't help you, perhaps you can paste a bit of the file "GuestBook.txt" so I can see how you are thinking. You shouldn't save the formatting in the file. I find it wise to seperate formatting/style and data. Hope this could help.

JayStang
01-06-2006, 11:39 PM
i'll give that a shot. If it helps any you can view the working guestbook.

http://ctechnow.com/jdulude/hardtosurrender/flash_files/index.html

The reason i wanted to use a txt is because its viewed in flash and that was the easiest way i knew of to read it in flash.

Element
01-06-2006, 11:42 PM
Anyway, my method stores the users inputed information in the file seperated by '|' so then we explode by that '|' when we retrieve the information, turning that string into a array.


$lines = file("GuestBook.txt");
foreach($lines as $line) {
$CommentData = explode("|", $line);
echo '<p>';

echo '<b>Name:</b. '.$CommentData[0].'<br />'; // the key 0 is the name because thats the first part of the string before the '|'
echo '<b>Email:</b> <a href="mailto:'.$CommentData[1].'">'.$CommentData[1].'</a><br />';
echo '<b>Website:</b> <a href="'.$CommentData[2].'">'.$CommentData[2].'</a><br />';

echo '<br />';

echo $CommentData[3]; // This is the last part of the line, which is the comment. Key number 3.

echo '</p>';

}



And that would be how you output the data stored like I showed you in my earlier post.

and you can use the styling method above also to achieve the look.

JayStang
01-07-2006, 12:34 AM
Thanks Element, I get what your saying. But i'm still missing the part where it changes the colors.

~Jamie

jskaar
01-07-2006, 12:42 AM
I see. I'd probably would divide each inlay as Element suggested. This will simplify styling a bit.

Kurashu
01-07-2006, 12:09 PM
Changing colors is easy, just make a quick change...


$lines = file("GuestBook.txt");
$i = 0;
foreach($lines as $line) {
$style = $i % 2 ? '' : ' class="odd-row"';//obviously change the class name to whatever you want
$CommentData = explode("|", $line);
echo '<p' . $style . '>';

echo '<b>Name:</b. '.$CommentData[0].'<br />'; // the key 0 is the name because thats the first part of the string before the '|'
echo '<b>Email:</b> <a href="mailto:'.$CommentData[1].'">'.$CommentData[1].'</a><br />';
echo '<b>Website:</b> <a href="'.$CommentData[2].'">'.$CommentData[2].'</a><br />';

echo '<br />';

echo $CommentData[3]; // This is the last part of the line, which is the comment. Key number 3.

echo '</p>';

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum