HomePHP 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.
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:
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.