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.

  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



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:

name CHAR(50) NOT NULL,
email CHAR(50) NOT NULL,
title CHAR(50) 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
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: