I have a forum that students are posting in. If the student responds to the main post, the postId is set to 0. If a student replies to another student's post, the postId is set to the replyId of the student's original post.
I am trying to write some PHP that will essentially create a new table for each post, with the exception of if a postid is set to a reply Id, then create a new row in that table.
I have the SQL laid out in SQLFiddle which can be found here:
http://sqlfiddle.com/#!2/611e2d/5
Using this example, what I'm looking for is replyid one to be put in a new html table, with response ID 3 in a new row underneath that. Then with ReplyId 2, would create a new html table.
This is just a basic threaded forum view of responses.
Thank you!
[code]
$i = 0;
$aR = 0;
$currentPost = '';
if(mysql_num_rows($getResponses) > 0)
{
while($respData = mysql_fetch_array($getResponses))
{
if(($respData['postId'] != $currentPost))
{
if($i!=0)
{
echo '</table><br /><br />';
}
echo '<table width = "875px" cellspacing = "0" cellpadding = "0" border = "0">';
$i=0;
}
$currentPost = $respData['postId'];
$color_A = 'class="altRow1"';
$color_B = 'class="altRow2"';
$altRowColor = ($aR % 2) ? $color_A : $color_B;
$studentName = getStudents($respData['userId']);
$studentName = explode(" ", $studentName);
$studentFirstName = $studentName[0];
echo '<tr ' . $altRowColor . '>
<td align="center" width = "225px" class="forumTopic"><img src="images/'.getStudentPics($respData['userId']).'.png" /><br />Posted By ' . getStudents($respData['userId']) . '<br />on '.date("m/d/Y h:i a", strtotime($respData['responseDate'])) . '</td>
<td width = "650px" class="forumTopic">' . $respData['replyText'] . '</td>
</tr>
<tr ' . $altRowColor . '>
<td class="forumTopic" colspan = "2" align="center"><span class="topicLinkStyle"><a href="postResponse.php?postId='.$respData['replyId'].'&topic='.$topicId.'" class="iframe750x600">Reply to '.$studentFirstName .'</a></span></td>
</tr>';
$i++;
$aR++;
}
echo '</table><br /><br />';
}
Here is a very simple example you can then make your own CSS to format the HTML anyway you want you can even use that link I posted on your comment as example.
The buildTree
will order the replies properly in a tree manner for easy later usage and the printTree
will print it recursively.
<?php
// Your database info
$db_host = '';
$db_user = '';
$db_pass = '';
$db_name = '';
if (!isset($_GET['topic_id']))
{
die('No topic id was given.');
}
$con = new PDO("mysql:host={$db_host};dbname={$db_name}", $db_user, $db_pass);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT replyId,
topicId,
postId,
replyText,
responseDate,
userId
FROM forumResponses
WHERE topicId = ?
ORDER BY replyId";
$result = $con->prepare($sql);
$result->bindParam(1, $_GET['topic_id'], PDO::PARAM_INT);
$result->execute();
if ($result->rowCount() == 0)
{
die('No messages found...');
}
$threads = array();
while($row = $result->fetchALL(PDO::FETCH_ASSOC))
{
$threads = $row;
}
$data = buildTree($threads);
$con = NULL;
function buildTree($ar, $pid = NULL) {
$op = array();
foreach($ar as $item)
{
if($item['postId'] == $pid)
{
$op[$item['replyId']] = array(
'replyText' => $item['replyText'],
'userId' => $item['userId'],
'responseDate' => $item['responseDate'],
'parentId' => $item['postId']
);
// using recursion
$children = buildTree($ar, $item['replyId']);
if($children)
{
$op[$item['replyId']]['children'] = $children;
}
}
}
return $op;
}
function printTree($ar)
{
foreach ($ar as $reply)
{
?>
<li>
<div>
<header><a href="javascript:void(0);">userId <?php echo $reply['userId']; ?></a> - <?php echo $reply['responseDate']; ?></header>
<?php echo $reply['replyText']; ?>
</div>
<?php
if (isset($reply['children']) && count($reply['children']) > 0)
{
echo " <ul>
";
printTree($reply['children']);
echo " </ul>
";
}
echo " </li>
";
}
}
?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Threaded Comments Block</title>
</head>
<body>
<div>
<h1>Reading Topic <?php echo $_GET['topic_id']; ?></h1>
<div>
<ul>
<?php
printTree($data);
?>
</ul>
</div>
</div>
</body>
</html>
NOTE: The above code is merely to show you an example of how store the result in a threaded manner as well as to print it, you will have to make your own out of this example.
I think it will be better to reconstruct the table for that..hmm here is my suggestion Table Post (suggested main table for responses of a topic)
Fields:
postId, topicId(topic where this post belongs), responseId(this will be the postId of the post if this post is a reply, set to 0 if main post),