PHP
  Home arrow PHP arrow Page 2 - Creating a Simple Threaded Discussion ...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Creating a Simple Threaded Discussion Forum
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 24
    2006-10-16

    Table of Contents:
  • Creating a Simple Threaded Discussion Forum
  • Index.php
  • viewarticle.php
  • Postre.php

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By Jacques Noah


       · Good tutorial, the system did work, although I would suggest cleaning up your code...
       · Well I do hope that everyone finds my article educational. I also thank you for...
       · Really this code i mean Creating a Simple Threaded Discussion Forum is looks prety ...
       · i am not master in php, but i want to become, i had seen ur post, where can i get...
       · I dont understand how it worked although i did what it told me to do it didnt do...
       · hello, i,m asking where to find the code for the files config.php,Tutstyle.css and...
       · Jacques - interesting article and I'd like to try your solution, but where can I get...
       · I am just starting to dig into the world of PHP development. I was so glad when I...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT