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.
The script "list.php" is designed to display a threaded list of all the comments posted for a specific article - it accomplishes this via the buildTree() function.
$connection = mysql_connect($hostname, $user, $pass) or die ("Unable to
// start building a comment tree
The buildTree() function is the guts of this application. It is a
recursive function that performs one query after another to create a threaded list of posts, starting from a specific branch of the tree and working its way progressively through all sub-branches. Take a look.
//this is the recursive function which is called to render the inner
children of every comment
function buildTree($id, $article, $section)
// get some variables
// select all comments for that article and section
// if an id is present, select comments which are branches on that comment
$query = "SELECT id, subject, timestamp, username FROM $table where article
= '$article' AND section = '$section' AND replytopost = '$id'";
$result = mysql_db_query($database, $query, $connection) or die ("Could not
execute query: $query. " . mysql_error());
// if at tree root and no comments available, display error
if(mysql_num_rows($result) <= 0 && $id == 0)
echo "<font face=Verdana size=2 color=Black><b>No comments
// display records
while(list($ID, $SUBJECT, $TIMESTAMP, $USERNAME) =
face=Verdana size=2 color=Black><b>$SUBJECT</b></a> </li>\n";
// this part is what causes the recursion -
// it exits the function
// when there are no more records(children) to render
echo "</ul>\n ";
Let's dissect this a little.
First, this function assumes that
a connection to the database has been created - if you take a look, you'll see that that is one of the first things "list.php" does. It also needs some variables - the name of the database and table to perform queries on, the level at which to start building the tree structure, and the article/section identifier (passed to it as parameters).
With all that information in place, buildTree() performs a SELECT query and obtains a list of "level 0" posts; it then prints these in a list. For each record thus returned, buildTree() then calls itself recursively with that record number, connects to the database again, and displays a list of all "level 1" posts. This process continues until the end of the tree is reached. The HTML list construct is used to ensure that different levels are indented appropriately. If no comments are found at level 0, a message indicating this is displayed.
As you might imagine, this process can certainly test the strength of a database server, especially if there are a large number of levels. That's why you should ensure that your system has enough processing power to handle this kind of multi-level query.
Each item in the list finally displayed is linked to the "details.php" page, which displays the complete content of the comment. Here's what it looks like:
This article copyright Melonfire 2001. All rights reserved.