Specifications

The replies will be stored in an array. Each reply will itself be a tree_node, that can contain an
array of replies to that article, which are themselves tree_nodes, and so on. This continues
until we reach the so-called leaf nodes of the tree, the nodes that do not have any replies. We
will then have a tree structure that looks like the one in Figure 29.1.
Some terminology: The message that we are replying to can be called the parent node of the
current node. Any replies to the message can be called the children of the current node. If you
imagine that this tree structure is like a family tree, this will be easy to remember.
The first article in this tree structurethe one with no parentis sometimes called the root node.
Building Practical PHP and MySQL Projects
P
ART V
714
This can be unintuitive because we usually draw the root node at the top of dia-
grams, unlike the roots of real trees.
NOTE
To build and display this tree structure, we will write recursive functions. (We discussed recur-
sion in Chapter 5, Reusing Code and Writing Functions.)
We decided to use a class for this structure because its the easiest way to build a complex,
dynamically expanding data structure for this application. It also means we have quite simple,
elegant code to do something quite complex.
Solution Overview
To really understand what we have done with this project, its probably a good idea to work
through the code, which well do in a moment. There is less bulk in this application than in
some of the others, but the code is a bit more complex.
There are only three real pages in the application.
We will have a main index page that shows all the articles in the forum as links to the articles.
From here, you will be able to add a new article, view a listed article, or change the way the
articles are viewed by expanding and collapsing branches of the tree. (More on this in a minute.)
From the article view, you will be able to post a reply to that article or view the existing replies
to that article.
The new article page enables you to enter a new post, either a reply to an existing message, or
a new unrelated message.
The system flow diagram is shown in Figure 29.2.
35 7842 CH29 3/6/01 3:34 PM Page 714