Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Formatting a txt file for a Guestbook? *help*

    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 Code:
    <?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($fp80000); 
      
    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$New800000); 
      
    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($fp800000); 
      
    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
    Last edited by JayStang; 01-06-2006 at 10:41 PM.

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    using the explode() function can help. Format your input like:

    PHP Code:
    $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:

    PHP Code:
    $CommentData explode("|"$line);

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


  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think the easiest way would be something like this:
    PHP Code:
    $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.

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'll give that a shot. If it helps any you can view the working guestbook.

    http://ctechnow.com/jdulude/hardtosu...les/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.

  • #6
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.
    PHP Code:
    $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.
    Last edited by Element; 01-06-2006 at 11:50 PM.

  • #7
    Regular Coder
    Join Date
    Jan 2006
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Element, I get what your saying. But i'm still missing the part where it changes the colors.

    ~Jamie
    Last edited by JayStang; 01-07-2006 at 01:16 AM.

  • #8
    New Coder
    Join Date
    Jan 2006
    Location
    Oslo, Norway
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see. I'd probably would divide each inlay as Element suggested. This will simplify styling a bit.

  • #9
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Changing colors is easy, just make a quick change...
    PHP Code:
    $lines file("GuestBook.txt");
    $i 0;
    foreach(
    $lines as $line) { 
      
    $style $i '' ' 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>'




  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •