DanielHardy Posted July 29, 2009 Share Posted July 29, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/ Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 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]; } } Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885787 Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 Thanks MadTech, I've had no luck with that though Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885792 Share on other sites More sharing options...
Adam Posted July 29, 2009 Share Posted July 29, 2009 What didn't work? Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885797 Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 No luck? so didn't work.. did anything display ? did you get an error ? Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885799 Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 no didn't work. No errors just a blank page displayed Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885803 Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 can you change $full = false; to $full = true; just to check EDIT: also a example of the messages.txt would be useful (in code tags) Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885811 Share on other sites More sharing options...
patrickmvi Posted July 29, 2009 Share Posted July 29, 2009 It would probably help if you posted the contents of the first five entries of messages.txt or a link to where we could download it to see how it looks inside and how you should parse it out. Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885815 Share on other sites More sharing options...
DanielHardy Posted July 29, 2009 Author Share Posted July 29, 2009 you can see the form in action at www.chcsc.org EDIT: add/messages.txt to see the file. Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885860 Share on other sites More sharing options...
MadTechie Posted July 29, 2009 Share Posted July 29, 2009 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]; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/167946-link-to-results-rather-than-automatically-displaying/#findComment-885891 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.