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.

742 Chapter 31 Building <strong>Web</strong> Forums<br />

Deciding how to display these relationships requires some careful thought. For this<br />

system, users should be able to view an individual message, a thread of conversation with<br />

the relationships shown, or all the threads on the system.<br />

Users must also be able to post new topics or replies.This is the easy part.<br />

Solution Components<br />

As we mentioned previously, the process of storing <strong>and</strong> retrieving the author <strong>and</strong> text of a<br />

message is easy.The most difficult part of this application is finding a database structure<br />

that will store the information you want <strong>and</strong> a way of navigating that structure efficiently.<br />

The structure of articles in a discussion might look like the one shown in Figure 31.1.<br />

Initial posting Reply 1<br />

Reply 1 to Reply 1<br />

Reply 2 to Reply 1<br />

Reply 2<br />

Reply 3<br />

Reply 1 to Reply 3<br />

Figure 31.1 An article in a threaded discussion might be the first article in a<br />

new topic, but more commonly it is a response to another article.<br />

In this diagram, you can see that the initial posting starts off a topic, with three replies.<br />

Some of the replies have replies.These replies could also have replies, <strong>and</strong> so on.<br />

Looking at the diagram gives you a clue as to how you can store <strong>and</strong> retrieve the<br />

article data <strong>and</strong> the links between articles.This diagram shows a tree structure. If you’ve<br />

done much programming, you’ll know that this is one of the staple data structures<br />

used. In the diagram, there are nodes—or articles—<strong>and</strong> links—or relationships between<br />

articles—just as in any tree structure. (If you are not familiar with trees as a data structure,<br />

don’t worry; we cover the basics as we go.)<br />

The tricks to getting this all to work are<br />

n Finding a way to map this tree structure into storage—in this case, into a <strong>MySQL</strong><br />

database<br />

n Finding a way to reconstruct the data as required

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

Saved successfully!

Ooh no, something went wrong!