我有一个学生发帖的论坛。如果学生回复主帖,则postId设置为0.如果学生回复另一个学生的帖子,则postId设置为学生原帖的replyId
我正在尝试编写一些PHP,它基本上会为每个帖子创建一个新表,但如果将postid设置为回复ID,则在该表中创建一个新行。
我在SQLFiddle中列出了SQL,可以在这里找到:
http://sqlfiddle.com/#!2/611e2d/5
使用这个例子,我正在寻找的是一个将被放入一个新的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 />';
}
答案 0 :(得分:2)
这是一个非常简单的示例,您可以创建自己的CSS来格式化HTML,无论如何您甚至可以使用that link I posted on your comment as example。
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>
注意:上面的代码仅仅是为了向您展示如何以线程方式存储结果以及打印它的示例,您必须自己做出这个示例。
答案 1 :(得分:0)
我认为重建表格会更好......这是我的建议 表格帖子(建议的主题回复主表)
字段:
postId,topicId(此帖子所属的主题),responseId(如果此帖子为回复,则为帖子的postId,如果是主帖子则设为0),