Administration
  Home arrow Administration arrow Page 6 - Talk To Me!
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ADMINISTRATION

Talk To Me!
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 1
    2001-03-12

    Table of Contents:
  • Talk To Me!
  • The Job
  • Building The Foundation
  • Mole In A Hole
  • Speak Now, Or Forever Hold Your Peace
  • Of Trees And Branches
  • Closing The Loop
  • The Last Word

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Talk To Me! - Of Trees And Branches
    (Page 6 of 8 )

    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 Administration Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    ADMINISTRATION ARTICLES

    - Configuring Load-Balanced Clusters
    - Load-Balanced Clusters
    - UNIX Time Format Demystified
    - Making Changes in the CVS
    - Building Your First CVS Repository
    - CVS Quickstart Guide
    - Authorizing Users in Samba
    - Handling User Accounts in Samba
    - Authentication in Samba
    - Accounts, Authentication, and Authorization
    - Advanced Concepts on Dealing with Files and ...
    - Dealing with Files and Filesystems
    - More Hacks for the User Environment in BSD
    - Personalizing the User Environment in BSD
    - Customizing the User Environment in BSD

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway