我有一个学生正在发帖的论坛。如果学生回复了主帖子,postId将设置为0。如果一个学生回复了另一个学生的帖子,postId将设置为该学生原始帖子的replyId。
我正在尝试编写一些PHP,它本质上会为每个帖子创建一个新表,但如果postid设置为回复Id,则在该表中创建一个新行。
我在SQLFiddle中列出了SQL,可以在这里找到:
http://sqlfiddle.com/#!2/611e2d/5
使用这个例子,我要寻找的是将一个replyid放在一个新的html表中,在它下面的新行中有响应ID 3。然后使用ReplyId 2,将创建一个新的html表。
这只是论坛对回应的基本看法。
谢谢!
[代码]
$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 />';
}
这里有一个非常简单的例子,你可以制作自己的CSS来格式化HTML,你甚至可以使用我在你的评论上发布的链接作为例子。
buildTree
将以树的方式对回复进行适当排序,以便于以后使用,printTree
将递归打印。
<?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>'n";
printTree($reply['children']);
echo " </ul>'n";
}
echo " </li>'n";
}
}
?>
<!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>
注意:上面的代码只是向您展示一个如何以线程方式存储结果以及打印结果的示例,您必须根据这个示例制作自己的结果。
我认为为此重建表会更好。。嗯,这是我的建议Table Post(建议的主题回复主表)
字段:
postId,topicId(这篇文章所属的主题),responseId(如果这篇文章是回复,这将是文章的postId,如果是主文章,则设置为0),