fbrown Posted July 7, 2007 Share Posted July 7, 2007 im trying to make a light simple guestbook for a guy who is hosting on my server and i didnt want to clutter my sql db so i am useing flat file so i make a test page to see if it works and the results were good but when i make the real page it doesnt work i went over it 2 or 3 times and i cant spot anything and im not getting any errors test page <?php $name = 'forrest'; $email = 'fbrown@yahoo.com'; $fp = fopen("bin/test.nfo","w"); if(!$fp) { echo 'Error: Cannot open file.'; exit; } fwrite($fp, $name."||".$email."\n"); fclose($fp); ?> <?php $userinfo = file("bin/test.nfo"); echo '<table border="0">'; foreach($userinfo as $key => $val) { //explode that data into a new array: $data[$key] = explode("||", $val); } for($k = 0; $k < sizeof($userinfo); $k++) { echo '<tr><td>'.$data[$k][0].' Wrote:</td></tr>'; echo '<tr><td>Email:</td><td> <table border=1 bordercolor=ffffff colspan=0 colpadding=0> <tr> <td bordercolor=black>'.$data[$k][1].'</td> </tr> </table> </td> </tr>'; echo '<tr><td colspan=2> </td></tr>'; } echo '</table>'; ?> the guest book <?php if (isset($_POST['name']) & ($_POST['email']) & ($_POST['comment'])) { $name = $_POST['name']; $email = $_POST['email']; $comment = $_POST['comment']; $date = date('D d Y h:i'); $fp = fopen("bin/guestbook.nfo","w"); if(!$fp) { echo 'Error: Cannot open file.'; exit; } fwrite($fp, $date."||".$name."||".$email."||".$comment."\n"); fclose($fp); } ?> <style type="text/css"> <!-- .style1 { color: #999999; font-weight: bold; } --> </style> <div align="center"> <h2><span class="style1">Sundance's Guest Book </span><br> </h2> </div> <form name="form1" method="post" action="<?php echo $url;?>"> <label>Name <input type="text" name="name"> </label> <label>Email <input type="text" name="textfield"> </label> <p> </p> <p> comment<br> <textarea name="comment" cols="40" rows="10"></textarea> <br> <label> <input type="submit" name="Submit" value="Sign"> </label> <br> </p> </form> <hr> <?php $comment = file("bin/guestbook.nfo"); echo '<table border="0">'; foreach($comment as $key => $val) { //explode that data into a new array: $data[$key] = explode("||", $val); } for($k = 0; $k < sizeof($userinfo); $k++) { echo '<tr><td>'.$data[$k][0].' Wrote:</td></tr>'; echo '<tr><td>Email:</td><td> <table border=1 bordercolor=ffffff colspan=0 colpadding=0> <tr> <td bordercolor=black>'.$data[$k][1].'</td> </tr> </table> </td> </tr>'; echo '<tr><td colspan=2> </td></tr>'; } echo '</table>'; ?> $url included in index.php <?php // find out the domain: $domain = $_SERVER['HTTP_HOST']; // find out the path to the current file: $path = $_SERVER['SCRIPT_NAME']; // find out the QueryString: if (isset($_SERVER['QUERY_STRING'])){ $queryString = $_SERVER['QUERY_STRING']; // put it all together: $url = "http://" . $domain . $path . "?" . $queryString;} else {$url = "http://" . $domain . $path ;} ?> the out put table doesnt make sence i know. if it works it should say "sat 07 2007 xx:xx Wrote" but im testing it at the moment but i cant see what isnt working if yall can spot what im missing it would be great Quote Link to comment Share on other sites More sharing options...
sinisake Posted July 8, 2007 Share Posted July 8, 2007 Hi, i have placed whole code on one place/in same file: <?php // find out the domain: $domain = $_SERVER['HTTP_HOST']; // find out the path to the current file: $path = $_SERVER['SCRIPT_NAME']; $url = "http://" . $domain . $path ; extract($_POST); if($Submit){ $date = date('D d Y h:i'); $comment=eregi_replace("\r\n","*",$comment); $comment=eregi_replace("\n","*",$comment); $fp = fopen("bin/guestbook.nfo","a+"); if(!$fp) { echo 'Error: Cannot open file.'; exit; } fwrite($fp, $date."||".$name."||".$email."||".$comment."\n"); fclose($fp); } ?> <style type="text/css"> <!-- .style1 { color: #999999; font-weight: bold; } --> </style> <div align="center"> <h2><span class="style1">Sundance's Guest Book </span><br> </h2> </div> <form name="form1" method="post" action="<?php echo $url;?>"> <label>Name <input type="text" name="name"> </label> <label>Email <input type="text" name="email"> </label> <p> </p> <p> comment<br> <textarea name="comment" cols="40" rows="10"></textarea> <br> <label> <input type="submit" name="Submit" value="Sign"> </label> <br> </p> </form> <hr> <?php $userinfo = file("bin/guestbook.nfo"); echo '<table border="0">'; foreach($userinfo as $val) { //explode that data into a new array: $data = explode("||", $val); echo '<tr><td>'.$data[0].'</td></tr>'; echo '<tr><td>'.$data[1].' Wrote:</td></tr>'; $data[3]=eregi_replace("\*","<br>",$data[3]); echo '<tr><td>'.$data[3].'</td></tr>'; echo '<tr><td>Email:'.$data[2].'<hr></td></tr>'; } echo '</table>'; ?> Some redundant parts are removed(especially in url include file-the path wasn't ok-why you need query string?) some errors too(you forgot to add name to 'email' text field in form, $userinfo var wasn't have any value, you forgot to change argument in fopen function to "a"...) Also, you'll see i tried to avoid biggest problem(if user press enter in comment field-new line!), and i hope it will work... Quote Link to comment Share on other sites More sharing options...
fbrown Posted July 8, 2007 Author Share Posted July 8, 2007 awsome it works lol i uh needed the query string because i use a content box so all of my links end in ?section=xy but its cool i am very much a newbe to php so if its not to much could you explain the output if not its cool thanks tho for figureing it out Quote Link to comment 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.