chandler Posted July 17, 2011 Share Posted July 17, 2011 Hi I have this comment script it will be placed on a few pages, each are written to .txt file so there will be messages1.txt, messages2.txt, messages3.txt and so on, is it possible using php to find out what was the last comment made and on what page, and then display this info on the main page some thing like last 5 comments made: made on page 3 17.07.2011 @ 15:00 made on page 2 16.07.2011 @ 15:00 etc the comment script <?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 $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); } } $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>"; ?> </div> Many Thanks for you help Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 17, 2011 Share Posted July 17, 2011 It sounds like you're trying to save each comment in a separate text file. If that the case, is there a reason why you can't store all the comments in a single text file? With the single text file, you would just need to read in the entire file, count the number of entries as you go, and display the last line to get what you want. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 17, 2011 Author Share Posted July 17, 2011 I thought this was the only way it could be done as I don't want the comments from page 1 mixing and being displayed with the comments from page 2 etc, I have been reading a bit about this tonight and its way over my head so I hope some one can help me..thanks Quote Link to comment Share on other sites More sharing options...
teynon Posted July 18, 2011 Share Posted July 18, 2011 You can do it the way you are talking about, but you might as well be reinventing the wheel. I would use a database. If your site ever did get a lot of visitors, all that file reading and writing will cause issues somewhere. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 wont be a problem its a closed site just for the family, plus I just wouldn't know where to begin to change this script to use a DB Quote Link to comment Share on other sites More sharing options...
chintansshah Posted July 18, 2011 Share Posted July 18, 2011 Hello Chander, For entering comments in db, you just create db connection on php page and then execute an query to save data to db. Write a query where you have write current file creation code. fwrite($fp, $comments); Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 18, 2011 Share Posted July 18, 2011 You are also gonna need a database server before using it. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 ok if I setup a db, can you please help me with it?, I think it will be better in the end as I will want a image/file upload feature... Thanks Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 18, 2011 Share Posted July 18, 2011 If you are working on windows WAMP is your best choice, especially if you dont have much experience installing/configuring separately MySQL/PHP/Apache environments. http://www.wampserver.com/en/ it is just practically pressing next -> next -> ready, and you got fully working systems. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 Ok I have setup a db, I have copied all the info ($mysql_host) etc and put it into a config.php file and I have placed an include config.php on the page. Now for setting up the query I have no idea how to do this, can you please help. Thanks Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 Yes I know I have used that before when I was trying to learn PHP but I had to uninstall it due to my computer crashing every time I ran it. I am still using the same computer its very old . Quote Link to comment Share on other sites More sharing options...
chintansshah Posted July 18, 2011 Share Posted July 18, 2011 Hi chandler, I am facing same problem then I have install xampp afterwards everything works smoothly and fast. Try xampp instead of wamp. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 Ok thanks I will try that now. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 Ok that is setup and seems to be working fine. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 This is what I have so far, how do I put the data into the db and also display it? <?php require_once('config.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 $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); } } $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>"; ?> Thank You Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 18, 2011 Share Posted July 18, 2011 Before continuing you'll want to learn how to use MySQL (mysql is the database server that comes with wamp/xampp). The tutorial here will help you to learn the basics of using MySQL with PHP. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 Ok I have read that, I am going to give it a go and see what mess I can make of this brb. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 18, 2011 Author Share Posted July 18, 2011 Ok I have made the table, // Create a MySQL table in the selected database mysql_query("CREATE TABLE comments( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30))") or die(mysql_error()); echo "Table Created!"; Should I see any rows / columns id, names etc, cos at the moment I only see table comments without any option to browse it. the complete code is, I know I have to remove some of the old code if any one can show me what I should remove it would be a big help ..thanks <?php require_once('config.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 $comments .= file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable. // Create a MySQL table in the selected database mysql_query("CREATE TABLE comments( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30))") or die(mysql_error()); echo "Table Created!"; // 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 Share on other sites More sharing options...
chandler Posted July 19, 2011 Author Share Posted July 19, 2011 I think I am close but can some one look this over, I'm not getting all the data from the db, I get the comment but no name?, and I am unsure if the data is coming from the db or the txt file. also I have lost all the css now how do I get that back? here is the code <?php require_once('config.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 $comments .= file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable. // Insert a row of information into the table "example" mysql_query("INSERT INTO comments (message, message2) VALUES('$message', '$message2' ) ") or die(mysql_error()); } } $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 // Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM comments") or die(mysql_error()); // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry echo "<div id=\"census41_messages\">"; echo "Name: ".$row['mesaage']; echo "Message: ".$row['message2']; echo "</div>"; ?> Thanks Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 19, 2011 Share Posted July 19, 2011 mysql_fetch_* functions return one row at a time. So when your query returns more than one row you need to use a while loop to get all the results. while($row = mysql_fetch_assoc( $result )) { // Print out the contents of the entry echo "<div id=\"census41_messages\">"; echo "Name: ".$row['mesaage']; echo "Message: ".$row['message2']; echo "</div>"; } Now that yo're using a database you no longer need these two lines. // 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 $comments .= file_get_contents('messages.txt'); // now we get all the existing comments and append it to the $comments variable. As your code stands now it should be saving and retrieving the posted comments from your database. You now need to validate/sanitize your user input to prevent SQL Injection/XSS attacks. Have a read of this four part guide for writing secure PHP code. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 19, 2011 Author Share Posted July 19, 2011 Hi thanks for that, I was just looking that up, my problems now are the same message is repeated and there is a huge space between the messages can you see from the code why its doing this? Thanks here is the new code <?php require_once('config.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'); // Insert a row of information into the table "example" mysql_query("INSERT INTO comments (message, message2) VALUES('$message', '$message2' ) ") or die(mysql_error()); } } $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 // Retrieve all the data from the "example" table $result = mysql_query("SELECT * FROM comments") or die(mysql_error()); // store the record of the "example" table into $row while($row = mysql_fetch_assoc( $result )) { // Print out the contents of the entry echo "<div id=\"census41_messages\">"; echo "<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>"; echo "</div>"; } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 19, 2011 Share Posted July 19, 2011 I cannot see why it is repeating messages/displaying large gaps. What I think is happening is you have a lot of duplicate/blank entries within your comments table. You will want to clear the contents of your comments table now. To clear the contents of the table run the following query in PHPMyAdmin TRUNCATE TABLE comments Then try posting a couple of new comments and see if does it again. Quote Link to comment Share on other sites More sharing options...
chandler Posted July 19, 2011 Author Share Posted July 19, 2011 after doing "TRUNCATE TABLE comments" all the comments were removed, but I am still getting the duplicated comment and the space. I guess this so I am sure it got something to do with this, do I need .$row ? if so how do I do that I did try kept giving errors echo "<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>"; Quote Link to comment Share on other sites More sharing options...
Zane Posted July 19, 2011 Share Posted July 19, 2011 I thought this was the only way it could be done as I don't want the comments from page 1 mixing and being displayed with the comments from page 2 etc, I have been reading a bit about this tonight and its way over my head so I hope some one can help me..thanks If you want to know for which page a comment is for, then put the page number on the same line as the comment. Easy as that. Wow, I didn't realize there was another page.. so I guess you went the db route. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 19, 2011 Share Posted July 19, 2011 after doing "TRUNCATE TABLE comments" all the comments were removed, but I am still getting the duplicated comment and the space. I guess this so I am sure it got something to do with this, do I need .$row ? if so how do I do that I did try kept giving errors echo "<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>"; Ooops. I didn't read your code properly earlier change the while loop to while($row = mysql_fetch_assoc( $result )) { $message = $row['message']; $message2 = $row['message2']; // Print out the contents of the entry echo "<div id=\"census41_messages\">"; echo "<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>"; echo "</div>"; } 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.