PHP
  Home arrow PHP arrow Page 4 - 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 - Displaying the forum: looking at the "fetchTitles()," fetchMessages()" and "createThreadForm()" methods


    (Page 4 of 5 )

    As you may have guessed, the core method of the class is "fetchTitles()," which is responsible for recursively navigating the "forum" database table and displaying the respective tree of nested threads. First, the method begins displaying all the main threads (the values for parent_id fields are equal to 0), and then goes as deep as required, in order to display the corresponding sub threads. You can learn how this is performed if you take a look at the signature of this method:

    function fetchTitles(){
        $result=$this->db->query("SELECT * FROM forum WHERE
    parent_id='$this->threadCode'");
        echo '<ul>';
        // loop over result set
        while($row=$result->fetchRow()){
            echo '<li><a href="'.$_SERVER['PHP_SELF'].'?
    threadcode='.$row['id'].'">'.$row['title'].'</a> Posted by
    ('.$row['name'].') '.$row['email'].'</li>';
            $this->threadCode=$row['id'];
            // call recursively the 'fetchTitles()' method
            $this->fetchTitles();
        }
        echo '</ul>';
    }

    In this method, you can clearly appreciate the role played by the aggregated $this->db object, which is handy when it comes to fetching all the thread titles from the database table. Undoubtedly, the most important thing to note here is the recursive implementation of the method, something extremely powerful and elegant for scanning the tree's branch nodes, in order to display all the sub threads.

    The remaining methods speak for themselves, so I won't spend much time on them. First, the "fetchMessages()" method is called when a thread title is clicked, and not surprisingly shows the contents of that particular thread. Its source code is listed below:

    function fetchMessages(){
        $result=$this->db->query("SELECT name,title,message FROM forum WHERE id='$this->threadCode'");
        if($result->countRows()==0){
            echo 'No messages were found!';
            return;                  
        }
        $row=$result->fetchRow();
        echo '<h2>'.$row['title'].'</h2><hr /><p>'.$row['name'].'
    wrote: '.$row['message'].'</p><a href="'.$_SERVER['PHP_SELF'].'?
    threadcode=0">Back to main threads</a>';
    }

    Finally, the "createThreadForm()" method is tasked with generating, on the fly, the online form that inserts new posts on the forum. Its definition is as follows:

    function createThreadForm(){
        $this->threadCode=intval($_GET['threadcode']);
        echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?
    threadcode='.$this->threadCode.'"><h2>Post your new
    message</h2>';
        echo 'Name <input type="text" name="user"
    size="30" /><br />Email <input type="text" name="email"
    size="30" /><br />';
        echo 'Title<input type="text" name="title"
    size="30" /><br />Message<br /><textarea  name="message"
    rows="10" cols="30"></textarea><br /><input type="submit"
    name="send" value="Add thread" /></form>';
    }

    In this case, the above method displays a basic web form, which after submitting itself, inserts a new message into the forum by the "addThread()" method. The source code for this one is listed below:

    function addThread(){
        $this->db->query("INSERT INTO forum
    (id,parent_id,name,email,title,message) VALUES (NULL,'$this-
    >threadCode','$this->user','$this->email','$this->title','$this-
    >message')");
        header('location:'.$_SERVER['PHP_SELF']);
    }

    Okay, I think that's all you need to learn about how the "ThreadProcessor" class works. Now, jump into the next section, in order to see the forum in action. It's really worthwhile.

    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

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - 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





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