Specifications
The most difficult part of this application is finding a database structure that will store the
information we want, and a way of navigating that structure efficiently.
The structure of articles in a discussion might look like the one shown in Figure 29.1.
Building Web Forums
C
HAPTER 29
29
BUILDING WEB
FORUMS
713
Initial posting Reply 1 Reply 1 to Reply 1
Reply 2
Reply 3 Reply 1 to Reply 3
Reply 1 to Reply 1
FIGURE 29.1
An article in a threaded discussion might be the first article in a new topic, but more commonly it is a response to
another article.
In this diagram, you can see that we have an initial posting starting off a topic, with three
replies. Some of the replies have replies. These replies could have replies, and so on.
Looking at the diagram gives us a clue as to how we can store and retrieve the article data and
the links between articles. This diagram shows a tree structure. If you’ve done much program-
ming, you’ll know that this is one of the staple data structures used. In the diagram there are
nodes—or articles—and links—or relationships between articles—just as in any tree structure.
(If you are not familiar with trees as a data structure, don’t worry—we will cover the basics as
we go.)
The tricks to getting this all to work are
1. Finding a way to map this tree structure into storage—in our case, into a MySQL database.
2. Finding a way to reconstruct the data as required.
We will begin by implementing a MySQL database that will enable us to store articles
between use.
We will build simple interfaces to enable saving of articles.
When we load the list of articles for viewing, we will load the headers of each article into a
tree_node PHP class. Each tree_node will contain an article’s headers and a set of the replies
to that article.
35 7842 CH29 3/6/01 3:34 PM Page 713