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

viewarticle.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).

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The screen shot above shows the viewarticles page listing the replies to a thread.  The function below is responsible for doing exactly that:

//view replies to selected threads -viewarticles.php
function
viewthread(){
      include("config.php");
        $query="select * from test where uid =$parent";
      if ($r =mysql_query($query)) {
 //Retrieve records
 while ($row =mysql_fetch_array($r)) {
//transfer values
 $child=$row['child'];
//$parent=$row['parent'];
$uid=$row['uid'];
$name=$row['name'];
$title=$row['title'];
$message=$row['message'];
$date=$row['date'];
//put values in session vars
$_SESSION['title']=$title;
$_SESSION['message']=$message;
$_SESSION['parent']=$parent;
$child=$_SESSION['parent'];
$child=$child + 1;
$_SESSION['child']=$child;
//display values
     echo "<b>Title:</b> $title<br> <b>By:</b> $name <br> <b>Date
Posted:</b> $date<br />";
         echo "<b>Message:</b> $message <br />";
         }
         }
         }

It's relatively straightforward. This function receives a $parent number from the index.php page and uses that number to retrieve all records with a uid equal to the  $parent value, as in:  $query="select * from test where uid =$parent";

Also, on the same page the code below is used to get all the replies related to the current thread:

$parent =$_GET['parent'];
include("config.php");
 $query="select uid,name,title,message,parent,date from test
where parent=$parent order by date,parent desc ";
 if ($r =mysql_query($query)) {
 //$num = mysql_num_rows($r);
 //Retrieve records
 while ($row =mysql_fetch_array($r)) {
$name=$row['name'];
$uid=$row['uid'];
$title=$row['title'];
$message=$row['message'];
$date=$row['date'];
//add bullet points
echo  '<ul type="disc">';
echo "<li>";
//echo "<a href="index.php?parent=$uid"> $title </a>";
echo "<a href="viewarticle.php?parent=$uid"> $title </a> ";
echo "-- by <b>$name</b> $date<br/>";
echo "</li></ul>";
}
}

This code receives the $parent value from the index page and uses that value to retrieve all records whose parent value matches the value in the $parent variable. I hope this is not too confusing. Say a new thread is created, and given a uid of 7. All the replies to this thread will have 7 in the parent column of the database. Therefore, if you want to find all the replies to this thread, all you have to do is to retrieve all the records that have seven in their parent column. And this is what happens here.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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