PHP
  Home arrow PHP arrow Page 2 - Developing a Discussion Forum in PHP w...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Developing a Discussion Forum in PHP with Recursion
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2006-05-15

    Table of Contents:
  • Developing a Discussion Forum in PHP with Recursion
  • Getting started with the forum: defining the structure of the MySQL database table
  • Processing forums threads: defining the "ThreadProcessor" class
  • Displaying the forum: looking at the "fetchTitles()," fetchMessages()" and "createThreadForm()" methods
  • The discussion forum in action: putting the "ThreadProcessor" class to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Developing a Discussion Forum in PHP with Recursion - Getting started with the forum: defining the structure of the MySQL database table


    (Page 2 of 5 )

    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


       · Want to see how recursion can be used in a real-world application? Then this article...
       · It's a great article, I found it really useful. I was trying to write a recursive...
       · Thanks a lot for your positive comments on this PHP article. I must say I feel glad...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway