You've seen how many Web sites (including this one) allow you topost comments and opinions on the material they publish. If you've everwished you had this capability on your Web site, or are simply curious asto how this is accomplished, read on.
This is a good time for you to download the source code, so that you can refer to it throughout this tutorial (you will need a Web server capable of running PHP and a mySQL database in order to run the application).
The first order of business is to design a table that will hold the comments entered by the user. After much thought, discussion and stale pizza, here is the structure we finally decided on.
#
# Table structure for table 'comments'
# comments.sql in the source archive
#
DROP TABLE IF EXISTS comments;
CREATE TABLE comments (
id int(11) NOT NULL auto_increment,
article int(11) DEFAULT '0' NOT NULL,
section int(11) DEFAULT '0' NOT NULL,
username varchar(100) NOT NULL,
email varchar(255),
timestamp timestamp(14),
subject varchar(255) NOT NULL,
post text NOT NULL,
replytopost int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (id)
);
#
# description of fields:
#
# id - unique numeric identifier for each comment in table
# article - comment belongs to this article
# section - article belongs to this section
# username - name of user posting comment
# email - email address of user posting comment
# timestamp - time of post (automatically generated)
# subject - subject of post
# post - body of post
# replytopost - if replying to existing post, numeric identifier of that
post (required for threading)
#
We decided that each post would display the author's name, email
address and date/time on which it was posted. Our original table did not include a field for the subject; this was added in order to make it easier for readers to quickly divine the nature of the post, and thereby decide whether or not it was worth reviewing.
In this structure, each post is identified by a number. This number, combined with the "replytopost" attribute, comes in very useful when constructing a threaded list of posts (as you'll see a little further down). The "replytopost" attribute identifies the post one level up in the discussion "tree", and thereby makes is easier to relate one post to another when constructing the threaded tree structure. A "replytopost" value of 0 indicates that there are no posts above this one in the tree...in other words, that this is a new "branch" of the discussion tree.
This article copyright Melonfire 2001. All rights reserved.