PHP
  Home arrow PHP arrow Page 4 - Developing a Discussion Forum in PHP with Recursion
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 12
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT