chandler Posted July 17, 2011 Share Posted July 17, 2011 Hi I have this comment script I am using on several pages. What I want is for the newest comment to appear on top, and if its possible I would like something on the main page telling the users where the last comment was made for example: last comment was made on page 3 17.07.2011 @ 15:00 I am rubbish at php so any help / links to help with the above would be much appreciated. <?php session_start(); ?> <?php if (isset($_POST['message'])) { if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) { $message = htmlentities($_POST['message']); $message2 = htmlentities($_POST['message2']); $message = stripslashes($_POST['message']); $message2 = stripslashes($_POST['message2']); $fp = fopen('messages.txt', 'a'); fwrite($fp, "<div id=\"comments_box\"><div id=\"comment_name\"><p>$message <em>Says:</em></div><br />$message2</p></div>"); fclose($fp); } } $token = md5(uniqid(rand(), true)); $_SESSION['token'] = $token; ?> <form id="contFrm" method="POST"> <input type="hidden" name="token" value="<?php echo $token;?>" /> <label><span class="required">*</span> Full Name:</label> <input type="text" class="box" name="message"><br /> <label><span class="required">*</span> Message: </label> <textarea name="message2" id="message" cols="25" rows="8"></textarea><br /> <input type="submit" class="button" value="Submit"> </form> <?php echo "<div id=\"census41_messages\"> "; readfile ('messages.txt'); echo "</div>"; ?> Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/ Share on other sites More sharing options...
QuickOldCar Posted July 17, 2011 Share Posted July 17, 2011 Do your comments in a database. You link comments to post id's. Here's something to get you in the right direction. http://www.zimmertech.com/tutorials/php/25/comment-form-script-tutorial.php Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/#findComment-1243784 Share on other sites More sharing options...
chandler Posted July 17, 2011 Author Share Posted July 17, 2011 Thanks for that but that's using a database I'd rather use the flat file with code I already have for the comments, it works well, is it not possible to order the comments in a flat file? Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/#findComment-1243794 Share on other sites More sharing options...
QuickOldCar Posted July 17, 2011 Share Posted July 17, 2011 Not without great effort. Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/#findComment-1243803 Share on other sites More sharing options...
wildteen88 Posted July 17, 2011 Share Posted July 17, 2011 To insert the latest post so it at the top you'll need to change the way your script adds the comments to messages.txt. As it is now your appending the latest comment to the file, which means the latest comment will always be added to the end of file. In order to add the latest comment so it at the top of the file you'll first need to get all the comments that is in messages.txt and then prepend the latest comment. Which will be like this: // add the newest comment to the start of the file $comments = "<div id=\"comments_box\"><div id=\"comment_name\"><p>$message <em>Says:</em></div><br />$message2</p></div>"; // this is the new comment to be added $comments .= file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable. // write all the comments back to the file with the newest comment first $fp = fopen('messages.txt', 'w'); fwrite($fp, $comments); fclose($fp); To add the date look into using the date function. Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/#findComment-1243805 Share on other sites More sharing options...
chandler Posted July 17, 2011 Author Share Posted July 17, 2011 Thanks for that here is the update code, but what I get now is the newest post replacing the last post. Have I done something wrong? <?php if (isset($_POST['message'])) { if (isset($_SESSION['token']) && $_POST['token'] == $_SESSION['token']) { $message = htmlentities($_POST['message']); $message2 = htmlentities($_POST['message2']); $message = stripslashes($_POST['message']); $message2 = stripslashes($_POST['message2']); ini_set('date.timezone', 'Europe/London'); // add the newest comment to the start of the file $comments = "<div id=\"comments_box\"><div id=\"comment_name\"><p>$message <em>Says:</em></div><div id=\"comment_date\">" . date ("D, M d, Y, g:i a") . "</div><br />$message2</p></div>"; // this is the new comment to be added $comnents .= file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable. // write all the comments back to the file with the newest comment first $fp = fopen('messages.txt', 'w'); fwrite($fp, $comments); fclose($fp); } } $token = md5(uniqid(rand(), true)); $_SESSION['token'] = $token; ?> <form id="contFrm" method="POST"> <input type="hidden" name="token" value="<?php echo $token;?>" /> <label><span class="required">*</span> Full Name:</label> <input type="text" class="box" name="message"><br /> <label><span class="required">*</span> Message: </label> <textarea name="message2" id="message" cols="25" rows="8"></textarea><br /> <input type="submit" class="button" value="Submit"> </form> <?php echo "<div id=\"census41_messages\">"; readfile ('messages.txt'); echo "</div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/#findComment-1243810 Share on other sites More sharing options...
wildteen88 Posted July 17, 2011 Share Posted July 17, 2011 $comnents .= should be $comments .=. I spelt comments wrong when I posted my code. Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/#findComment-1243812 Share on other sites More sharing options...
chandler Posted July 17, 2011 Author Share Posted July 17, 2011 ops I didn't spot that either, nice job works perfect..Thank you Quote Link to comment https://forums.phpfreaks.com/topic/242183-comment-script-question/#findComment-1243816 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.