Creating a Simple Threaded Discussion Forum - viewarticle.php
(Page 3 of 4 )

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.
Next: Postre.php >>
More PHP Articles
More By Jacques Noah