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

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



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
    echo '<ul>';
    // loop over result set
        echo '<li><a href="'.$_SERVER['PHP_SELF'].'?
threadcode='.$row['id'].'">'.$row['title'].'</a> Posted by
('.$row['name'].') '.$row['email'].'</li>';
        // call recursively the 'fetchTitles()' method
    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'");
        echo 'No messages were found!';
    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(){
    echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?
threadcode='.$this->threadCode.'"><h2>Post your new
    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-

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

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: