Home arrow PHP arrow Page 2 - Creating a Simple Threaded Discussion Forum

Index.php - PHP

Most websites have some method of interacting with a visitor. Some have a chat system and others have shoutboxes or other ways to attract the visitor to return. One of the most used methods is a discussion forum. And this is what we are going to create and discuss in this article (the first of two parts).

  1. Creating a Simple Threaded Discussion Forum
  2. Index.php
  3. viewarticle.php
  4. Postre.php
By: Jacques Noah
Rating: starstarstarstarstar / 39
October 16, 2006

print this article



The first page in this application will display all the main threads. See image below:

To display the new threads, we run a database query that retrieves all topics with a parent ID equal to 0 (remember we said that all new threads will be equal to 0?). In addition, we will also create a pagination system that will allow us to view only a set number of threads per page. So the gettopics function below does two things: it shows us the main threads, and it sets the page number. Have a look at the function code below:

function gettopics($topicid){
$Per_Page=5; //number of topics to show on a page
 // Run The Query Without a  Limit to get Total result
 $SQL="SELECT COUNT(*) AS Total FROM test where parent=0 ";
$SQL_Result=mysql_db_query($dbname, $SQL);
  // Create a new SELECT Query with the
   // ORDER BY clause and without the COUNT(*)
$SQL="SELECT * FROM test where parent=$topicid ORDER BY date
//Append a LIMIT clause to the sql statement
 if (empty($_GET['Result_Set'])) {
 $SQL.=" LIMIT $Result_Set, $Per_Page";
 $SQL.=" LIMIT $Result_Set, $Per_Page";
 // Run The Query With a Limit to get result
echo "<table width="760" >";
echo "<tr bgcolor="#3399CC">
<td width="190"><center><b>Subject</b></center></td>
<td width="190"><center><b>Name</b></center></td>
<td width="190"><center><b>Date</b></center></td>
<td width="190"><center><b>Replies</b></center></td>";
echo '</tr>';
echo "</table>";
 // Display Results using a for loop
 for ($a=0; $a < $SQL_Rows; $a++) {
//query to get all replies
$query2="select * from test where parent=$uid order by
date,parent desc ";
 $num = mysql_num_rows($result);
$num = mysql_num_rows($result);
 if($num >0){
 $replies= "$num";
 $image="<img src="fb.gif">";
 $replies= "0";
 $image="<img src="images/doc.gif">";
 echo "<table width="760" >";
echo "<tr>
      <td width="190">$image<a href="viewarticle.php?
parent=$uid"> $title </a> </td>
    <td width="190"><b>$name</b></td>
    <td width="190">$date</td>
    <td width="190"><b>$replies</b> replies <br/></td>";
 echo '</tr>';
echo "</table>";
echo '<br>';
echo '<div id="Pageno">';
 // Create Next / Prev Links and $Result_Set Value
 if ($Total>0)
 if ($Result_Set<$Total&& $Result_Set>0) {
echo '<A HREF="index.php?Result_Set='.$Res1.'"> << Previous
Page</A> ';
 // Calculate and Display Page # Links
 $Pages=$Total / $Per_Page;
 if ($Pages>1) {
  for ($b=0,$c=1; $b < $Pages; $b++,$c++) {
   $Res1=$Per_Page * $b;
 echo '<A HREF="index.php?Result_Set='.$Res1.'"> '.$c.'</A> ';
 if ($Result_Set>=0 && $Result_Set<$Total) {
if ($Res1<$Total) {
echo '<A HREF="index.php?Result_Set='.$Res1.'"> Next Page >></A>';
echo '</div>';

Let me explain how this function works. First the function deals with the pagination issue. The $Per_Page variable sets the number of records you want to display on a page. The function also builds the table that will host all of the records and data. In addition, it also retrieves the replies made to all the individual threads. The function looks difficult but is really very simple; there are comments on the code to make it easy for you to understand what is going on.

>>> More PHP Articles          >>> More By Jacques Noah

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: