Specifications
When we construct the root treenode from display_tree() from the main page, we are actu-
ally creating a -dummy node with no article associated with it. We pass in some initial values:
$tree = new treenode($start, ‘’, ‘’, ‘’, 1, true, -1, $expanded, $sublist);
This creates a root node with a $postid of zero. This can be used to find all the first-level
postings because they have a parent of zero. We set the depth to -1 because this node isn’t
actually part of the display. All the first-level postings will have a depth of zero, and be at the
far left of the screen. Subsequent depths step towards the right.
The most important thing that happens in this constructor is that the children nodes of this
node are instantiated. We begin this process by checking if we need to expand the children
nodes. We only perform this process if a node has some children, and we have elected to dis-
play them:
if(($sublist||$expand) && $children)
{
$conn = db_connect();
We then connect to the database, and retrieve all the child posts, as follows:
$query = “select * from header where parent = $postid order by posted”;
$result = mysql_query($query);
We then fill the array $m_childlist with instances of the treenode class, containing the
replies to the post stored in this treenode, as follows:
for ($count=0; $row = @mysql_fetch_array($result); $count++)
{
if($sublist||$expanded[ $row[‘postid’] ] == true)
$expand = true;
else
$expand = false;
$this->m_childlist[$count]= new treenode($row[‘postid’],$row[‘title’],
$row[‘poster’],$row[‘posted’],
$row[‘children’], $expand,
$depth+1, $expanded, $sublist);
}
This last line will create the new treenodes, following exactly the same process we have just
walked through, but for the next level down the tree. This is the recursive part. A parent tree
node is calling the treenode constructor, passing its own postid as parent, and adding one to
its own depth before passing it.
Each treenode in turn will be created and create its own children until we run out of replies or
levels that we want to expand to.
Building Web Forums
C
HAPTER 29
29
BUILDING WEB
FORUMS
729
35 7842 CH29 3/6/01 3:34 PM Page 729