j0mby Posted February 27, 2007 Share Posted February 27, 2007 I'm trying to make a very simple comments page on my site using PHP and am having problems somewhere. I am very new to PHP. I was able to create one that works with comments appended, but I want the latest comment to be on top, and that's where I'm running into trouble. Since I know very little about PHP, I thought I was clever in what I came up with. I think it can work if I get the coding right. Let me know if my logic is wrong. I'm working on a mac OS X and writing it all in simple text, but when I have time at work, I am messing around in Notepad on a PC. I get almost all of my code from tutorials and some here and there from forums. What I've done is start with a simple form. Then it goes to the first comments page, comments.php This form retrieves the form data, opens the first text file (comments.txt) and reads it. Then it opens the second file (comments2.txt) and is supposed to write the form data at the top and the already saved data (from comments.txt) underneath it. This file also writes the form data to a database (which seems to be working) and is also supposed to echo 'thanks for your comment, $name (which isn't working). Then after 5 seconds it sends you to the second PHP doc. The second PHP doc (fanscomments.php) then opens the comments2.txt file and echos it and also opens the comments.txt doc and writes over it with comments2.txt In this way, I was thinking it should result in the latest comment posting on the top when the whole loop is completed. I know this is a clumsy way of doing this, but as I said, I'm new to coding and wanted to have something functional, and then replace it later with something more sophisticated. Here is the first PHP page: <?php if(!empty($_SERVER['HTTP_REFERER'])) header('Refresh: 5,URL="/fanscomments.php"'); // retrieve form data $input = $_POST['name']; $input = $_POST['eaddress']; $input = $_POST['comments']; // define variables $name = "$_POST[name]"; $comments = "$_POST[comments]"; $eaddress = "$_POST[eaddress]"; echo 'Thanks for your comment, $name!'; // set file to read $file = '/home/content/s/c/o/scodeath/html/fans/comments.txt'; // open file $fh = fopen($file, 'r') or die('Could not open file!'); // read file contents $commentsdata = fread($fh, filesize($file)) or die('Could not read file!'); // close file fclose($fh); $out = fopen("/home/content/s/c/o/scodeath/html/fans/comments2.txt", "w"); if (!$out) { print("Could not append to file. Please try again later, or send an email to fans@summercampofdeath.com"); exit; } fputs($out,"\"$_POST[comments]\"\n\n"); fputs($out,"-$_POST[name]\n\n\n\n"); fwrite($out, $commentsdata); fclose($out); $out = fopen("/home/content/s/c/o/scodeath/html/fans/commentsdb.txt", "a"); if (!$out) { print("Could not append to file. Please try again later, or send an email to fans@summercampofdeath.com"); exit; } fputs($out,"$_POST[name]\t"); fputs($out,"$_POST[eaddress]\t"); fputs($out,"$_POST[comments]\t"); fputs($out,"$_SERVER[REMOTE_ADDR]\n\n"); fclose($out); ?> And here is the second PHP page: <html> <head></head> <body> <?php // set file to read $file = '/home/content/s/c/o/scodeath/html/fans/comments2.txt'; // read file into array $data = file($file) or die('Could not read file!'); // loop through array and print each line foreach ($data as $line) { echo nl2br($line); } $out = fopen("/home/content/s/c/o/scodeath/html/fans/comments.txt", "w"); if (!$out) { print("Could not append to file. Please try again later, or send an email to fans@summercampofdeath.com"); exit; } fputs($out,"$data\n\n"); fclose($out); ?> </body> </html> I sure appreciate the help! BTW, why is it printing the word - Array in my webpage? Thanks! Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted February 27, 2007 Share Posted February 27, 2007 Is there any reason you're not using a database like MySQL? Quote Link to comment Share on other sites More sharing options...
j0mby Posted February 27, 2007 Author Share Posted February 27, 2007 I was assuming it would be harder (and take longer) to accomplish, since I don't know much about mysql. I was only trying to "throw it up there", and then probably, down the road, replace it with one that uses a mysql database. but if it's just a few more steps to do that, and the way I'm trying to do it now is silly, I'd take your advice and do some mysql tutorials, etc. But I was also curious as to why what I'm trying to do isn't working. I've learned a lot about php by trying it, so it's not all a loss. Quote Link to comment Share on other sites More sharing options...
artacus Posted February 27, 2007 Share Posted February 27, 2007 I was assuming it would be harder (and take longer) You are incorrect in your assumptions. Quote Link to comment Share on other sites More sharing options...
j0mby Posted February 27, 2007 Author Share Posted February 27, 2007 OK. So you recommend I take the time to code this using a mysql database, so I'll check out some tutorials. Thanks. However, can anyone tell if what I was trying to do would work if I could get the coding right? I don't know why it won't write the post data along with string/array data into the file... anyway, thanks for the help. Quote Link to comment Share on other sites More sharing options...
magnetica Posted February 27, 2007 Share Posted February 27, 2007 Sorry to be annoying but: I get almost all of my code from tutorials and some here and there from forums. I'm new to coding If your new to coding and you piece your work together then: 1) You will always come here for help because you don't know how your scripts work 2) You will always piece things together and never learn Quote Link to comment Share on other sites More sharing options...
j0mby Posted February 27, 2007 Author Share Posted February 27, 2007 thanks for the "help", magnetica. can anyone else actually help me with my code instead of judging me for not being an ace at php coding after 3 weekends of messing around with it? Any real advice is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
artacus Posted February 27, 2007 Share Posted February 27, 2007 Yeah, you could save your data to a flat file. But it wont be as efficient. And the time you take learning how to do it would be much better spent learning how to use mysql... which really isn't too hard. Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted February 27, 2007 Share Posted February 27, 2007 Mysql is extremely simple, it was the first thing I really learned how to do in PHP, and its easy to order things how you want them: lets say I have a list in a mysql table called...list_table, and two rows inside called id and list: [ id ] [ list ] 1 php rules 2 I'm cool 3 apples are yummy A very dumb list, but a list nonetheless, to pull this out of the database, you would have to first connect to the database, which I will name db_test: $link = mysql_connect("mysql hostname", "username", "password") or die(mysql_error()); mysql_select_db("db_test") or die(mysql_error()); Ok, you're connected. Now you need to pull out the data from the table called list, do this by executing a mysql query: $data = mysql_query('SELECT * FROM `list_table`'); This stores all the information in a resource identifier called $data, in ascending order (biggest bottom, smallest top), to get it in descending order, (smallest bottom, biggest top) do this: $data = mysql_query('SELECT * FROM `list_table` ORDER BY `id` DESC'); Now to print it out, do this: while($info = mysql_fetch_array($data)) { echo $info['list']."<br />"; } If you used the ascending order one, you will get this output: php rules I'm cool apples are yummy if you used the descending order one, you will get this: apples are yummy I'm cool php rules I hope this helps. Quote Link to comment Share on other sites More sharing options...
j0mby Posted February 28, 2007 Author Share Posted February 28, 2007 Man! That's exactly what I needed. Thanks HaLo2FrEeEk! And I appreciate the critique of my previous plan, atacus. I will definitely take the time to check out mysql. thanks guys. this forum has been helpful. 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.