Home arrow PHP arrow Page 2 - Developing a Discussion Forum in PHP with Recursion

Getting started with the forum: defining the structure of the MySQL database table - PHP

If you’re interested in learning how to use recursion in PHP, look no further. Welcome to the third (and last) tutorial of the series “Recursion in PHP.” In three parts, this series walks through the fundamentals of recursive functions in PHP, in addition to explaining how to define and utilize recursive methods in object-based applications.

TABLE OF CONTENTS:
  1. Developing a Discussion Forum in PHP with Recursion
  2. Getting started with the forum: defining the structure of the MySQL database table
  3. Processing forums threads: defining the "ThreadProcessor" class
  4. Displaying the forum: looking at the "fetchTitles()," fetchMessages()" and "createThreadForm()" methods
  5. The discussion forum in action: putting the "ThreadProcessor" class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
May 15, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To start building this simple discussion forum, what I'll do first is define the structure of the MySQL database table that will serve for storing user data, such as messages, user names, email addresses, and so on. At the same time it will be used to construct a logical tree, in such a way that it can be traversed by a recursive function (or method, to be more accurate), in order to display all this data.

Keeping in mind the database structure that I just explained above, here is the SQL code for creating the "forum" database table:

CREATE TABLE forum
(
id INT(4) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
parent_id INT(4) UNSIGNED NOT NULL,
name CHAR(50) NOT NULL,
email CHAR(50) NOT NULL,
title CHAR(50) NOT NULL,
message TEXT NOT NULL
);

As you can see, the above sample "forum" database table is comprised of six fields: id, parent_id, name, email, title and message respectively, which are quite descriptive about the type of data they will house. However, let me stop for a while on the "parent_id" field, since it's important to know the meaning of it. Essentially, the values this field will store will be the ID of the corresponding parent node within the structure of a linked tree, so if a particular forum thread has no associated parent (placed on top of the tree), then obviously this value will be 0. In a similar fashion, the different branching threads will be generated by assigning the corresponding parent IDs to each row, as the tree structure goes deeper and deeper.

To illustrate how the above linked tree works, here is a schematic example of the "forum" database table, populated with a few records:

In the above example, the database table contains six forum threads, where the two first are main threads, that is their parent_id fields are equal to 0, then the two subsequent ones are sub threads of threads 1 and 2 (parent_id=2 and parent_id=1), and finally the last threads are also sub threads of threads 3 and 4 respectively. Quite understandable, right?

As you can see, a linked tree structure is pretty easy to be constructed by using a simple database table, and based on this tree structure, the discussion forum will work properly.

Now that you know how the "forum" database looks, it's time to leap forward and begin coding the PHP class responsible for making the forum work as expected. To see how this will be achieved please read the next few lines.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: