Well, there are two (at least two.. I could probably list a few more) methods to do this. Firstly, using files. The latter method would be to use MySQL. Both are fairly simple, yet probably above an extremely novice programmer's level. Here are descriptions of what to do.
Create the "comment" form, first. As in, what information do you want to be sent? From here, we can work on what to add in the code. Lets assume all you want is a comment and a name, very much like a guest book. Lets also realise that you have multiple comment pages, and that you'll have to assign a name or number to each page. The obvious way would be to assign it after a page name, or after a news ID, or something similar.
Assuming that you're using this on news only, we'll go with the news ID labelling method. Each news post will have a specific ID for it, possibly returned from a MySQL query. If not, we can make one: each news post is followed one after the other, so just inc a global variable:
$newsid ++;
From here, we can create the link to the comment script. Here's another chance to go two ways. The comment script can either be two scripts (read and write), or one script (?page=read, ?page=write). The latter method isn't all that much harder, but you'll have to alter all of your code to put it in a conditional statement. Well, if you want neat code.
You can choose which way you want to do this, I'm sure you know how to do conditional statements. If not, check out the manual.
Now, back to where we were: the link. After you've retrieved your news ID and printed out the news article, you can simply print out a link:
<a href="comments.php?id=$id">Read Comments</a> |
<a href="writecomments.php?id=$id">Write Comments</a>
Time to go back to the very beginning, where you created the form to send information. This will be the aforementioned writecomments.php, unless you want to use the switch method. It might be nice work habit to make it two files originally and then combine them, however.
How does the script know that information has been sent? Well, you'll need to name some of your form elements. For instance, everything you plan on having sent in will need a name, and the submit button will also need a name. I bet you're wondering why the submit button needs a name, right? Well, this is the most secure way to check if someone is submitting the form. You'll also need a hidden field named "id," which contains the news ID from earlier.
Now that everything is named correctly, you'll have to set up a conditional statement to check if they've pressed the submit button yet or not. If they have, send the information; conversely, if they have not, display the form.
Time for some actual coding: submitting the information. At the very beginning of this post, I said that there are a handful of ways to do this, including using files and MySQL. Many servers now will give you MySQL access, but just in case, I'll tell you both.
If you don't know how to use files in PHP, this will be a great way to learn. You may want to check the manual if you need additional help, however.
Using fopen(): fopen() simply means "open this file, call it this variable." Unfortunately, it's not entirely that simple. It has certain modes with which one can open a file. These are all listed in the manual, but I'll tell you which ones you'll need for this specific project. The "a+" mode means to read and write, and put the pointer at the beginning of the file. If you're on Windows, you may also need "b," which makes sure it's writing to the file in the correct format (ASCII or binary).
All in all, this is what your code for opening the file should look like:
$commentfile = fopen("comments$id","a+b");
You'll now need to write your new information to the file, using fwrite(). This function is extremely simple, and should be easy to grasp. All you need to supply it with is the file pointer and the text:
fwrite($commentfile,"$text");[code=php]
Of course, if you want this to look even somewhat decent, you'll need a table or something with which to match it with your layout. I'll leave you to writing the HTML, but here's how you do it. Lets assume the fields for text and name are $text and $name, respectively.
[code=php]$writetext = "$text<br><hr>-$name"; // Simple enough, huh?
Now, close the file. Use fclose() to close it, and just supply it with the file pointer: fclose($commentfile);
Now you need to be able to read the files for comments.php. This is pretty easy, and I'll bet you can understand it straight from the start:
$commentsfile = fopen("comments$id","r");
$text = fread($commentsfile,filesize("comments$id"));
fclose($commentsfile);
echo($text);
But there's an even easier way: include(). This is a one-line function that replaces everything on the reading end.
Now, MySQL.
First, you'll need the table for holding every comment:
CREATE TABLE comments(id INT, contents TEXT, name TEXT)
Then, in writecomments, you'll need to query to write the comment:
$result = mysql_query("INSERT INTO comments (id,contents,name) VALUES($id,'$text','$name');") or die("Error.");
And, in the file that reads comments, something to query and output the information:
$result = mysql_query("SELECT * FROM comments WHERE id=$id") or die("Error.");
while ($line = mysql_fetch_array($result, MYSQL_NUM)) {
echo($line[1]."<br><hr> - ".$line[2]);
}
Congratulations, you now know how to make a comment script. I hope this is helpful. (If you read it all.. 🙂)