Creating a Simple Threaded Discussion Forum - Index.php (
Page 2 of 4 )
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){
include("config.php");
$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);
$SQL_Result_Array=mysql_fetch_array($SQL_Result);
$Total=$SQL_Result_Array['Total'];
// Create a new SELECT Query with the
// ORDER BY clause and without the COUNT(*)
$SQL="SELECT * FROM test where parent=$topicid ORDER BY date
desc";
//Append a LIMIT clause to the sql statement
if (empty($_GET['Result_Set'])) {
$Result_Set=0;
$SQL.=" LIMIT $Result_Set, $Per_Page";
}
else
{
$Result_Set=$_GET['Result_Set'];
$SQL.=" LIMIT $Result_Set, $Per_Page";
}
// Run The Query With a Limit to get result
$SQL_Result=mysql_db_query($dbname,$SQL);
$SQL_Rows=mysql_num_rows($SQL_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++) {
$SQL_Array=mysql_fetch_array($SQL_Result);
$title=$SQL_Array['title'];
$name=$SQL_Array['name'];
$uid=$SQL_Array['uid'];
$message=$SQL_Array['message'];
$date=$SQL_Array['date'];
//query to get all replies
$query2="select * from test where parent=$uid order by
date,parent desc ";
$result=mysql_query($query2);
$num = mysql_num_rows($result);
$num = mysql_num_rows($result);
if($num >0){
$replies= "$num";
$image="<img src="fb.gif">";
}else{
$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) {
$Res1=$Result_Set-$Per_Page;
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) {
$Res1=$Result_Set+$Per_Page;
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.