PHP
  Home arrow PHP arrow Page 2 - Creating a Simple Threaded Discussion Forum
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Creating a Simple Threaded Discussion Forum
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 29
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek