Home arrow Site Administration arrow Page 6 - Talk To Me!

Of Trees And Branches - Administration

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.

TABLE OF CONTENTS:
  1. Talk To Me!
  2. The Job
  3. Building The Foundation
  4. Mole In A Hole
  5. Speak Now, Or Forever Hold Your Peace
  6. Of Trees And Branches
  7. Closing The Loop
  8. The Last Word
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 1
March 12, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

<? // list.php include("config.php"); include("common.php"); $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); ?> <html> <head> </head> <body> <hr> <? // start building a comment tree buildTree(0,$article, $section); ?> <hr> </body> </html>

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 global $connection; global $database; global $table; // select all comments for that article and section // if an id is present, select comments which are branches on that comment "tree" $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 available</b></font>"; } else { // display records while(list($ID, $SUBJECT, $TIMESTAMP, $USERNAME) = mysql_fetch_row($result)) { echo "<ul>\n"; echo "<li><a href=details.php?id=$IDžion=$section&article=$article><font 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 buildTree($ID,$article, $section); 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.

 
 
>>> More Site Administration Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

SITE ADMINISTRATION ARTICLES

- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: