Jump to content

link to results rather than automatically displaying


DanielHardy

Recommended Posts

 

Hi,

 

I have the following guestbook code up and running perfectly. However, the guestbook area currently occupies a small area on an already full page. I was hoping for guidance on how I would go about displaying the first say 5 results, and then offerring a link to a page that would display all of the results. Any sort of help is appreciated.

 

<?php
  
   $guestbook = "messages.txt";

  
   if (isset($_POST['button']))
   {
  
      if (!empty($_POST['name'])  && !empty($_POST['message']))
      {
  
         $badSearch = array("@", ".");
  
         $goodSearch = array(" @ ", " . ");
  
         $string .= "<b>" . $_POST['name'] . "  ";
           $string .= str_replace($badSearch, $goodSearch, $_POST['email']) . "</b>\n";
  
         $string .= "<font color=#2766c5>" . $_POST['message'] . "</font>\n<hr>";
         
  
         $file = fopen($guestbook, "a");
           fwrite($file, nl2br(strip_tags($string, "<b><hr><font>")));
  
         fclose($file);
         echo '<script>alert("Your comment has been added ")</script>';
      } else {
  
         echo '<script>alert("Please fill out the whole form")</script>';
      }
   }
   
  
  
   $readfile = fopen($guestbook, "r");
  
   echo @fread($readfile, filesize($guestbook));
  
   fclose($readfile);

?>

Thanks

 

Dan

 

okay this is the part that displays the entries

   $readfile = fopen($guestbook, "r");
   echo @fread($readfile, filesize($guestbook));
   fclose($readfile);

 

try something like this

$full = false; //condition for testing 5 lines

if($full)
{
   $readfile = fopen($guestbook, "r");
   echo @fread($readfile, filesize($guestbook));
   fclose($readfile);
}else{
   $lines = file($guestbook);
   foreach ($n=0;$n<=5;$n++) {
      echo $lines[$n];
   }
}

I changed the way it builds the text file and also change to display the LAST 5 message

<form id="form1" name="form1" method="post" action="">
  <label>
  
  <div align="center">

    <table width="100" border="0">
      <tr>
        <td height="20"><p>
          <strong><center>Name</strong><br />
          <input type="text" name="name" id="name" />
          <br />
          <br />
          
          <p><strong>Message</strong><br />

            <textarea name="message" id="message" cols="16" rows="6"></textarea>
          </p>
          <p>
          <input type="submit" name="button" id="button" value="Submit" />
          <input type="reset" name="button2" id="button2" value="Reset" />
        </p></td>
      </tr>
    </table>

  </div></label>
</form>

<?php
$guestbook = "messages.txt";
if (isset($_POST['button']))
{
if (!empty($_POST['name'])  && !empty($_POST['message']))
{
	$badSearch = array("@", ".");
	$goodSearch = array(" @ ", " . ");
	$string .= "<b>" . $_POST['name'] . "  ";
	$string .= str_replace($badSearch, $goodSearch, $_POST['email']) . "</b>";
	$string .= "<font color=#2766c5>" . $_POST['message'] . "</font><hr>\n";
	$file = fopen($guestbook, "a");
	fwrite($file, nl2br(strip_tags($string, "<b><hr><font>")));
	fclose($file);
	echo '<script>alert("Your comment has been added ")</script>';
} else {
	echo '<script>alert("Please fill out the whole form")</script>';
}
}
$full = false; //condition for testing 5 lines

if($full)
{
   $readfile = fopen($guestbook, "r");
   echo @fread($readfile, filesize($guestbook));
   fclose($readfile);
}else{
   $lines = file($guestbook);
   $lastline = count($lines);
   for($n=$lastline-5;$n<=$lastline;$n++) {
      echo $lines[$n];
   }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.