13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution Overview<br />

743<br />

For this project, you begin by implementing a <strong>MySQL</strong> database that enables you to store<br />

articles between each use.You then build simple interfaces to enable saving of articles.<br />

When you load the list of articles for viewing, you will load the headers of each article<br />

into a tree_node <strong>PHP</strong> class. Each tree_node will contain an article’s headers <strong>and</strong> a<br />

set of the replies to that article.<br />

The replies will be stored in an array. Each reply will itself be a tree_node that can<br />

contain an array of replies to that article, which are themselves tree_nodes, <strong>and</strong> so on.This<br />

process continues until you reach the so-called leaf nodes of the tree, the nodes that do not<br />

have any replies.You then have a tree structure that looks like the one in Figure 31.1.<br />

Some terminology:The message that you are replying to can be called the parent node<br />

of the current node. Any replies to the message can be called the children of the current<br />

node. If you imagine that this tree structure is like a family tree, this terminology will be<br />

easy to remember.<br />

The first article in this tree structure—the one with no parent—is sometimes called<br />

the root node.<br />

Note<br />

Calling the first article the root can be unintuitive because you usually draw the root node at the top of<br />

diagrams, unlike the roots of real trees.<br />

To build <strong>and</strong> display the tree structure for this project, you will write recursive functions.<br />

(We discussed recursion in Chapter 5,“Reusing Code <strong>and</strong> Writing Functions.”)<br />

We decided to use a class for this structure because it’s the easiest way to build a complex,<br />

dynamically exp<strong>and</strong>ing data structure for this application. It also means you have<br />

quite simple, elegant code to do something quite complex.<br />

Solution Overview<br />

To really underst<strong>and</strong> this project, it’s probably a good idea to work through the code,<br />

which we do in a moment.This application has less bulk than some of the others, but<br />

the code is a bit more complex.<br />

This application contains only three real pages. It has a main index page that shows all<br />

the articles in the forum as links to the articles. From here, you can add a new article,<br />

view a listed article, or change the way the articles are viewed by exp<strong>and</strong>ing <strong>and</strong> collapsing<br />

branches of the tree. (More on this shortly.) From the article view page, you can post<br />

a reply to that article or view the existing replies to that article.The new article page<br />

enables you to enter a new post, either a reply to an existing message or a new, unrelated<br />

message.<br />

The system flow diagram is shown in Figure 31.2.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!