以下代码从表CHART和COMMENTS中获取来自MySQL数据库的注释消息(状态)。当您发表评论时,代码首先出现问题,评论出现在消息下面(这没关系,所有评论都必须在评论的消息下排序,从最旧到最新)。
但是,当您再次发表评论时,它会创建一个新的对话消息和新评论,而不是在您发送的第一条评论下添加评论。有人可以帮我解决这个问题吗?或者更好的方法是什么?
<?php
$profile= htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
$reply_acc= htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
$result = mysqli_query($con, "SELECT ch.msg , ch.msg_id , co.comment , co.comment_id FROM chart AS ch, comments AS co WHERE ch.msg_id=co.comment_id");
while($row = mysqli_fetch_array($result))
{
echo("
<table border='1' width='600px'>
<tr>
<td>
$row[msg]
</td>
</tr>
<tr>
<td>
$row[comment]
<p></p>
<form action='drop_comment.php' method='post'>
<input type='text' name='comment' placeholder='drop a comment...' value='' class='add_hook'>
<input name='comment_id' type='hidden' value='$row[msg_id]'>
<input name='id' type='hidden' value='$row[msg_id]'>
<input name='comment_via' type='hidden' value='$device'>
<input name='comment_time' type='hidden' value='$status_time'>
</form>
</td>
</tr>
</table>
<p></p>
");
}
?>
答案 0 :(得分:0)
按消息排序查询,然后发表评论。然后只在更改时打印邮件以对邮件进行分组。
$result = mysqli_query($con, "SELECT ch.msg , ch.msg_id , co.comment , co.comment_id
FROM chart AS ch
JOIN comments AS co
ON ch.msg_id=co.comment_id
ORDER BY ch.msg_id, co.comment_id");
$last_msg = null;
while($row = mysqli_fetch_array($result))
{
if ($row['msg_id'] !== $last_msg) {
if ($last_msg !== null) {
echo "</table>\n<p><p>\n";
}
echo "<table border='1' width='600px'>
<tr>
<td>
$row[msg]
</td>
</tr>\n";
$last_msg = $row['msg_id'];
}
echo "
<tr>
<td>
$row[comment]
<p></p>
<form action='drop_comment.php' method='post'>
<input type='text' name='comment' placeholder='drop a comment...' value='' class='add_hook'>
<input name='comment_id' type='hidden' value='$row[msg_id]'>
<input name='id' type='hidden' value='$row[msg_id]'>
<input name='comment_via' type='hidden' value='$device'>
<input name='comment_time' type='hidden' value='$status_time'>
</form>
</td>
</tr>
");
}
echo "</table>\n<p><p>\n";