driz Posted November 20, 2008 Share Posted November 20, 2008 Hi, I'm trying to reverse the lines in my text file, here is my code: $filename = "comments.txt"; $date = date('jS F Y'); $time = date('h:i a'); $name = $_POST['name']; $email = $_POST['email']; $website = $_POST['website']; $message = $_POST['message']; $submit = $_POST['submit']; // when user submits the form if($submit=="submit") { // open text file $fp=fopen($filename, 'a+'); // then using this format $fstring="||" . $date . "||" . $time . "||" . $name . "||" . $email . "||" . $website . "||" . $message . "\n\r"; // write the comment fwrite($fp, $fstring); // close text file fclose($fp); }; This is what I have come up with: fputs ($fp, array_reverse ($fstring)); I have added it here: // open text file $fp=fopen($filename, 'a+'); // then using this format $fstring="||" . $date . "||" . $time . "||" . $name . "||" . $email . "||" . $website . "||" . $message . "\n\r"; // write the comment fwrite($fp, $fstring); // reverse the comments fputs ($fp, array_reverse ($fstring)); // close text file fclose($fp); The comments don't reverse though! They still show the original way?Thanks. x Link to comment Share on other sites More sharing options...
jeffman Posted November 21, 2008 Share Posted November 21, 2008 array_reverse() expects to be passed an array. Go figure. I guess it's treating your string like an array with one item. One item is the same forwards as backwards.Assuming you have a really really good reason for doing all this, you could explode() your string into an array using your "||" mark as the delimiter, then array_reverse() it, then implode() it back into an string.But really, if you explained your project, we can probably suggest a better way to get where you want to go. Link to comment Share on other sites More sharing options...
driz Posted November 21, 2008 Author Share Posted November 21, 2008 Hi, the project is a guestbook:here is the code in full <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Guestbook</title> <link rel="stylesheet" type="text/css" href="stylesheet.css" /> <?php /* To do: Bad word filter DONE Login Admin Pagination Alternating Comments DONE Reverse Order Comments */ $filename = "comments.txt"; $date = date('jS F Y'); $time = date('h:i a'); $name = $_POST['name']; $email = $_POST['email']; $website = $_POST['website']; $message = $_POST['message']; $submit = $_POST['submit']; // when user submits the form if($submit=="submit") { // open text file $fp=fopen($filename, 'a+'); // then using this format $fstring="||" . $date . "||" . $time . "||" . $name . "||" . $email . "||" . $website . "||" . $message . "\n\r"; // write the comment fwrite($fp, $fstring); // close text file fclose($fp); }; ?> <script type="text/javascript"> function checkForm(form) { if (form) { var fout_nr = 0; var fout = ''; var filter=/^.+@.+\..{2,3}$/; if (form.name.value == '') { fout_nr++; fout += '- Name\n'; } if (!filter.test(form.email.value)) { fout_nr++; fout += '- Email\n'; } if (form.message.value == '') { fout_nr++; fout += '- Message\n'; } if (fout_nr > 0) { alert('Please fill in the following fields correctly:\n' + fout); return false; } else { return true; } } else { return false; } } </script> </head> <body> <div id="wrapper"> <div id="sidebar"> <div id="header"> <h1>PHP Guestbook</h1> <h2>by CAMERON DRYSDALE (u0558234)</h2> </div> <form id="guestbook" name="guestbook" method="post" action="index.php" onsubmit="return checkForm(this)"> <fieldset> <legend>Add Comment</legend> <ul> <li> <label for="name"><strong>•</strong> Name</label> <input type="text" name="name" id="name" size="30" /> </li> <li> <label for="email"><strong>•</strong> Email</label> <input type="text" name="email" id="email" size="30" /> </li> <li> <label for="website">Website</label> <input type="text" name="website" id="website" size="30" /> </li> <li> <label for="message"><strong>•</strong> Message</label> <textarea name="message" id="message"></textarea> </li> <li> <label for="submit"> </label> <button type="submit" class="submitBtn" value='submit' name='submit'><span>Post Comment</span></button> <button type="reset" class="resetBtn"><span>Cancel</span></button> </li> <ul> <p><strong>•</strong> = must be filled in</p> </fieldset> </form> <div id="footer"> <p>© 2008 CAMERON DRYSDALE (u0558234)</p> </div> </div><!-- /sidebar--> <div id="content"> <div class="padding"> <?php // start with container for comments echo("<div class=\"listcomments\">"); // if user submits a comment if($submit!="submit") { // get comments from the text file $filecontents=file($filename); // sets what happens for each line in the text file foreach($filecontents as $line) { // gets rid of the seperators in the text file $data=explode("||" , $line); // words to filter $profanity=array("dammit", "git", "crap", "bloody", "bugger"); // replacement words $censorship=array("d****t", "g*t", "c**p", "b****y", "b****r"); //$censorship="*BEEP*"; // replaces swear words with new words and stores the clean message in filtered variable $filtered=str_ireplace($profanity, $censorship, $data[6]); // smilies $smilies=array(":)", ":(", ":p"); // replacement images $emoticons=array("<img src=\"smilies/smile.png\" />", "<img src=\"smilies/sad.png\" />", "<img src=\"smilies/tongue.png\" />"); // check for blank entries before displaying entries if (trim($line) != "") { $i++; if($i&1) { echo("<div class\"comment\">"); //echo("<div class=\"message\"><p>" . $data[6] . "</p></div>"); echo("<div class=\"message\"><p>$filtered</p></div>"); echo("<div class=\"author\">"); echo("<p><img src=\"img/avatar.png\" class=\"user\" /><span><a href=\"mailto:$data[4]\">" . $data[3] . "</a></span>"); echo("" . $data[1] . " @ " . $data[2] . "<br />"); echo("" . $data[5] . "</p>"); echo("</div>"); echo("</div>"); } else { echo("<div class\"commentalt\">"); //echo("<div class=\"messagealt\"><p>" . $data[6] . "</p></div>"); echo("<div class=\"message\"><p>$filtered</p></div>"); echo("<div class=\"authoralt\">"); echo("<p><img src=\"img/avataralt.png\" class=\"useralt\" /><span><a href=\"mailto:$data[4]\">" . $data[3] . "</a></span>"); echo("" . $data[1] . " @ " . $data[2] . "<br />"); echo("" . $data[5] . "</p>"); echo("</div>"); echo("</div>"); } } }; }; // if user has clicked the submit button, display thankyou message if($submit=="submit") { echo("<div class=\"alert\"><p>Thanks! Your comment has been added.</p></div>"); }; // closes the comments container echo("</div>"); ?> </div><!-- /padding--> </div><!-- /content--> </div><!-- /wrapper --> <div id="toolbar"> <p><a id="admin" title="Admin" href="admin.php">Admin</a></p> <p><a id="refresh" title="Refresh" href="index.php">Refresh</a></p> </div> <div id="toolbar-two"> <p><a id="up" title="Top of Page" href="java script:scroll(0,0);">top</a></p> </div> </body></html> Link to comment Share on other sites More sharing options...
jeffman Posted November 21, 2008 Share Posted November 21, 2008 Well, the code might come in handy. I was thinking of an explanation in words.Why would you want to write the same data twice, but one version is reversed? Anyway. A guestbook. Has anyone suggested storing this stuff in a database? Because what you are essentially creating is a database. In the long run you'll end up with a lot more flexibility. Link to comment Share on other sites More sharing options...
driz Posted November 21, 2008 Author Share Posted November 21, 2008 Well, the code might come in handy. I was thinking of an explanation in words.Why would you want to write the same data twice, but one version is reversed? Anyway. A guestbook. Has anyone suggested storing this stuff in a database? Because what you are essentially creating is a database. In the long run you'll end up with a lot more flexibility.Hi, its a University assignment and has to be Flat File based, no database stuff :)Take a look at http://paperview.org/guestbook/ to see my development version.Basically I was wanting the message at the bottom to be a the top and vice versa, so it reads from newest to oldest rather than oldest to newest as it is doing at the moment. So that's why I was using that array_reverse!Hope this help. x Link to comment Share on other sites More sharing options...
jeffman Posted November 21, 2008 Share Posted November 21, 2008 Gotcha on the school requirements. Since you are writing the file like a database, it doesn't matter how it's stored. It only matters how you read it when you get around to displaying it.The file() function reads the entire file into an array. Once you have that, you can reverse the array, or more simply read the individual records from the bottom, using array_pop.---Or, if you want to write the new record to the beginning of the file, you'll have to do this:open the file for "r+"read the whole file into a variable and save itrewind the file pointerwrite the new record to the file (you'll be at the beginning of it because of the rewind)write the saved data after thatEven if your original array_reverse plan had worked, it would only have reversed the new record. It wouldn't have reversed the whole file, because you hadn't read the file. This method will. Link to comment Share on other sites More sharing options...
driz Posted November 21, 2008 Author Share Posted November 21, 2008 Gotcha on the school requirements. Since you are writing the file like a database, it doesn't matter how it's stored. It only matters how you read it when you get around to displaying it.The file() function reads the entire file into an array. Once you have that, you can reverse the array, or more simply read the individual records from the bottom, using array_pop.---Or, if you want to write the new record to the beginning of the file, you'll have to do this:open the file for "r+"read the whole file into a variable and save itrewind the file pointerwrite the new record to the file (you'll be at the beginning of it because of the rewind)write the saved data after thatEven if your original array_reverse plan had worked, it would only have reversed the new record. It wouldn't have reversed the whole file, because you hadn't read the file. This method will.I think I'm a little confused, but here is my take: $varone = array("one", "two", "three", "four"); $vartwo = array_pop($varone); echo("$vartwo"); This would echo four three two one? When I tried it, it just outputted four and not the rest of the array!My problem is how to do this with my guestbook, getting the whole comment into one array? $filecontents is just a variable right? Unless I'm missing the point :S Link to comment Share on other sites More sharing options...
jeffman Posted November 21, 2008 Share Posted November 21, 2008 Obviously I should have looked more closely at the code, since you're already doing some of the things I talked about. Like, $filecontents already is an array that holds your complete file. So that's the thing (not $fstring) that you want to reverse before printing. (Forget about pop for right now.) Try this: // get comments from the text file$filecontents=file($filename);$filecontents = array_reverse ($filecontents); // ADD THIS LINE HERE ---For future reference, pop functions work like this: $arr = array("one", "two", "three", "four");$str = array_pop ($arr);// $str now equals "four", while $arr equals ("one", "two", "three") Most PHP functions don't alter their arguments, or if they do you have to pass them a reference (&$var), but array_pop acts directly on the argument. It makes sense if you think about what it's doing. Link to comment Share on other sites More sharing options...
driz Posted November 21, 2008 Author Share Posted November 21, 2008 Awesome, I knew I was right about the filecontents being the key, just didn't quite get the reverse array, but it makes perfect sense when you put it all together I need to do some pagination now, so basically show 10 comments and then another 10 on the next page. Do you know of any good resources for learning how to do something like this?Thanks. x Link to comment Share on other sites More sharing options...
jeffman Posted November 21, 2008 Share Posted November 21, 2008 You could Google it. Most of the responses assume you're using MySql, so adapting them could be a pain.Since the length of your $filecontents array is the number of entries, that's where it all begins. For simple pagination, divide that by 10 (or whatever) to get the number of pages. Each of your links would have a query string that contains an index for your array. Your PHP script uses the index to extract the correct entries for printing and to create the links. You can get fancier, but that's the basic idea. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.