Hello,
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!