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

The discussion forum in action: putting the "ThreadProcessor" class to work - 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

Assuming that you understood the logic implemented by the "ThreadProcessor" class, below I developed an example that shows the discussion forum in action. For the sake of completeness, I also included the classes that connect to MySQL and process result sets. Here is the corresponding sample code:

class MySQL {
    var $conId; // connection identifier
    var $host; // MySQL host
    var $user; // MySQL username
    var $password; // MySQL password
    var $database; // MySQL database
    // constructor
    function MySQL($options=array()){
        // validate incoming parameters
        if(count($options)>0){
            foreach($options as $parameter=>$value){
                if(empty($value)){
                    trigger_error('Invalid parameter
'.$parameter,E_USER_ERROR);
                }
                $this->{$parameter}=$value;
            }
            // connect to MySQL
            $this->connectDB();
        }
        else {
            trigger_error('No connection parameters were
provided',E_USER_ERROR);
        }
    }
    // connect to MYSQL server and select database
    function connectDB(){
        if(!$this->conId=mysql_connect($this->host,$this-
>user,$this->password)){
            trigger_error('Error connecting to the
server',E_USER_ERROR);
        }
        if(!mysql_select_db($this->database,$this->conId)){
            trigger_error('Error selecting
database',E_USER_ERROR);
        }
    }
    // perform query
    function query($query){
        if(!$this->result=mysql_query($query,$this->conId)){
            trigger_error('Error performing query
'.$query,E_USER_ERROR);
        }
        // return new Result object
        return new Result($this,$this->result); 
    }
}

class Result {
    var $mysql; // instance of MySQL object
    var $result; // result set
    function Result(&$mysql,$result){
        $this->mysql=&$mysql;
        $this->result=$result;
    }
    // fetch row
    function fetchRow(){
        return mysql_fetch_array($this->result,MYSQL_ASSOC);
    }
    // count rows
    function countRows(){
        if(!$rows=mysql_num_rows($this->result)){
            return false;
        }
        return $rows;
    }
    // count affected rows
    function countAffectedRows(){
        if(!$rows=mysql_affected_rows($this->mysql->conId)){
            trigger_error('Error counting affected
rows',E_USER_ERROR);
        }
        return $rows;
    }
    // get ID from last inserted row
    function getInsertID(){
        if(!$id=mysql_insert_id($this->mysql->conId)){
            trigger_error('Error getting ID',E_USER_ERROR);
        }
        return $id;
    }
    // seek row
    function seekRow($row=0){
        if(!mysql_data_seek($this->result,$row)){
            trigger_error('Error seeking data',E_USER_ERROR);
        }
    }
    function getQueryResource(){
        return $this->result;
    }
}

// connect to MySQL
$db=&new MySQL(array('host'=>'host','user'=>'user','password'=>'password',
'database'=>'database'));
// instantiate 'ThreadProcessor' object
$threadProc=&new ThreadProcessor($db);
// display forum threads
echo $threadProc->displayThreads();

As shown in the above script, after including the couple of MySQL processing classes, all I needed to do to get the forum working was instantiate a "ThreadProcessor" object and call its "displayThread()" method. Wasn't that simple?

Also, below I included an example screen shot, which illustrates the discussion forum in action, after populating the "forum" database table with a few trivial messages:

As depicted above, the "ThreadProcessor" class does a decent job traversing recursively the "forum" database table and displaying main and sub threads, as well as the respective web form for posting new messages. I hope the above image will be clear enough to demonstrate the power of recursion in PHP.

Final thoughts

In this last article of the series, I showed you how to use recursion in a practical example: the development of a "quick and dirty" discussion forum. I left as homework coding the class methods that update and delete forum threads, but I'm sure you'll have a clear idea of how to do this, since both database operations are very simple to translate to PHP code. As usual, see you in the next PHP tutorial!



 
 
>>> 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 11 - Follow our Sitemap

Dev Shed Tutorial Topics: