我正在为我们正在构建的一个简单的社交网络系统制作一个类似 Facebook 的墙贴。到目前为止,除了显示评论之外,它运行得很好。在我的数据库中,我有一个用于帖子的表和另一个用于评论的表。我在特定帖子中显示评论的方式是,在我的评论表中,有一个名为 comment_id 的列。因此,例如,如果帖子的 id 是 7 并且 comment_id 也是 7,那么评论将出现在具有相同 id 的帖子下方。现在,我的问题是我可以成功显示各自帖子中的评论,但只显示 1 条。我希望所有评论都显示在它们所属的每个帖子中,但我似乎无法使其发挥作用。
<?php
require_once 'dbconfig.php';
$user = $_SESSION['user'];;
echo '<form action="post.php" method="post" class="wallpost"><input
type="text" name="post" size="50"><input type="submit" name="wallpost"
value="Post"></form><br/>';
$query = $con->query("SELECT * FROM statuspost LEFT JOIN comments ON
statuspost.id=comments.comment_id group by statuspost.id DESC");
while($i = $query->fetch_object()){
echo '<span class="user">'.$i->user.'</span>'.'<br>'.'<span
class="post">'.$i->post.'</span>'.' <form action="editpost.php?type=post&
id='.$i->id.'" method="post"><span id="edit'.$i->id.'" class="editfield"
style="display:none;"><input type="text" name="edit"><input
type="submit"
value="Edit" class="editb"><br/></span><a href="#"
onclick="showEdit(edit'.$i->id.');">Edit </a><a
href="remove.php?type=post&
id='.$i->id.'" >Remove</a><a href="comment.php"> Comment</a></form>'.'<br
/>'.'<form action="comment.php?for='.$i->id.'" method="post"
id="comment">
<input type="text" name="comment" id="comment">
<input type="submit" value="Comment" id="commentb"></form>'.'<br
/><ul><li>'.$i->comment.'</li></ul>';
}
?>
最佳答案
So, example if the id of the post is 7 and the comment_id is also 7 then the comment will appear below that post with the same id. [...] but only 1 is being displayed
如果您将评论与帖子 ID 链接起来,则每个帖子只会收到一条评论,因为您只能有一个唯一的帖子 ID(假设您将帖子 ID 作为主键)。
您的 comments
表中应该有一个 post
列,并让该列引用他们评论的帖子。
当您进行查询时,您可以将评论与帖子内部连接
,如下所示:
INNER JOIN comments ON comments.post = posts.id
关于php - 如何用mysql记录填充无序列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29135316/