only for RuBoard - do not distribute or recompile Previous Section Next Section

Solution Components

As we've said previously, storing and retrieving the author and text of a message is easy.

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.

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.
graphics/29fig01.gif

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 programming, 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.

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 structure—the one with no parent—is sometimes called the root node.

Note

This can be unintuitive because we usually draw the root node at the top of diagrams, unlike the roots of real trees.



To build and display this tree structure, we will write recursive functions. (We discussed recursion in Chapter 5, "Reusing Code and Writing Functions." )

We decided to use a class for this structure because it's 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.

only for RuBoard - do not distribute or recompile Previous Section Next Section