我的问题是如何根据每个帖子中的评论对回复进行分组。我是PHP的初学者,我通过练习和给自己任务来学习。我正在开发一个使用评论系统作为其功能一部分的博客。我发现很难根据评论对回复进行分组。
我已经能够通过创建帖子 id 的外键来根据帖子寻求评论。我尝试使用相同的方法根据评论寻求回复。也就是说,我希望每个回复都位于相应的评论下。我在回复表中创建了一个 (comments_id) 的外键,并将其链接到注释表的 id。 因此,我使用下面的查询根据评论寻求回复,其中显示了整个回复。在最后一条评论之后显示相同数量的回复的每个帖子,即使他们没有达到该数量的回复。我有 5 条回复,3 条回复 1 条评论,其他 1 条评论各 2 条回复。但是在每个帖子的最后一条评论的末尾,将显示 5 条回复。 但是当我将查询中的第一行更改为:$r_query = $get_replies = "选择 * 从回复中,其中 comments_id = $id";没有什么会被取代。 评论.php和帖子.php如下所示。我决定不删除代码的任何部分,因为每个部分都可能是必要的。
$r_query = $get_replies = "SELECT * FROM replies ";
<?php
$r_query = $get_replies = "SELECT * FROM replies";
$r_run = mysqli_query($con,$r_query);
if(mysqli_num_rows($r_run) <= 1){
?>
<div class="comment">
<?php
while($r_row = mysqli_fetch_array($r_run)){
$r_id = $r_row['id'];
$r_name = $r_row['name'];
$r_username = $r_row['username'];
$r_image = $r_row['image'];
$r_reply = $r_row['reply'];
?>
<div class="row single-comment user-comment">
<div class="col-sm-2">
<img src="img/<?php echo $r_image; ?>" alt="Profile Picture" class="rounded-circle">
</div>
<div class="col-sm-10">
<h4><?php echo ucfirst($r_name); ?></h4>
<span class="time"><?php echo date('M j, Y', strtotime($r_row['date'])); ?></span>
<span class="time"><?php echo 'at '.date('h:i a', strtotime($r_row['date'])); ?></span>
<p><?php echo $r_reply; ?></p>
</div>
</div>
<?php }?>
</div>
<?php }?>
带有示例数据的注释表结构
创建表comments
(id
int(11) 不为空,date
日期时间默认值空,name
瓦尔查尔(255) 不为空,username
瓦尔查(255) 不为空,post_id
int(11) 不为空,email
瓦尔查尔(255) 不为空,website
瓦尔查尔(255) 不为空,image
瓦尔查尔(255) 不为空,comment
文本不为空,status
瓦尔查尔(255) 不为空,posts_id
int(11) 不为空 ) 引擎=InnoDB 默认字符集=拉丁语1;
插入comments
(id
、date
、name
、username
、post_id
、email
、website
、image
、comment
、status
、posts_id
)值 (136, '2019-02-08 01:16:17', 'Val Amasiatu
-- 表comments
的索引
更改表comments
添加主键 (id
), 使用BTREE添加密钥comments and posts link
(posts_id
); -- 转储表的AUTO_INCREMENT
-- 表comments
AUTO_INCREMENT
更改表comments
修改id
INT(11) 不空 AUTO_INCREMENT, AUTO_INCREMENT=155; -- 转储表的约束
-- 表comments
的约束
更改表comments
在更新级联上删除级联Foreign key to posts table
外键(posts_id
)引用posts
(id
)添加约束; 犯;
包含示例数据的回复表结构
创建表replies
(id
int(11) 不为空,date
日期时间不为空,name
瓦尔查尔(255) 不为空,username
瓦尔查尔(255) 不为空,email
瓦尔查尔(255) 不为空,image
瓦尔查尔(255) 不为空,comment
文本不为空,status
瓦尔查尔(255) 不为空,comments_id
int(11) 不为空 ) 引擎=InnoDB 默认字符集=拉丁语1; -- 转储表replies
的数据 插入replies
(id
、date
、name
、username
、email
、image
、comment
、status
、comments_id
)值 (163, '2019-02-08 23:02:23', 'Vall Amasia', 'val', 'cgeneral@gmail.com', '成人-胡须-模糊-927022.jpg', '第一次回复', '批准', 151),
-- 表replies
的索引
更改表replies
添加主键 (id
), 添加密钥Comment and replies relationship
(comments_id
); -- 转储表的AUTO_INCREMENT
-- 表replies
的AUTO_INCREMENT
更改表replies
修改id
INT(11) 不空 AUTO_INCREMENT, AUTO_INCREMENT=168; -- 转储表的约束 -- 表replies
的约束 更改表replies
在更新级联上删除级联Foreign key to replies table
外键(comments_id
)引用comments
(id
)添加约束; 犯;
帖子表的结构和示例数据
创建表posts
(id
int(11) 不为空,postDate
日期时间不为空,title
瓦尔查尔(255) 不为空,author
瓦尔查尔(255) 不为空,author_image
瓦尔查尔(255) 不为空,image
瓦尔查尔(255) 不为空,categories
瓦尔查尔(255) 不为空,tags
瓦尔查尔(255) 不为空,post_data
文本不为空,views
int(11) 不为空,status
瓦尔查尔(255) 不为空 ) 引擎=InnoDB 默认字符集=拉丁语1; -- 转储表posts
的数据 插入posts
(id
、postDate
、title
、author
、author_image
、image
、categories
、tags
、post_data
、views
、status
)值 (42, '2019-02-03 11:21:22', '什么是Lorem Ipsum?', '1234', 'AvePoint.jpg', 'admin-balance-blur-272978.jpg', '教程', '我的,我的,我的', '
但是我无法做的是找到并将这个丢失的结束标签div放在它应该在的地方。
', 8, '发布'),-- 表posts
的索引
更改表posts
添加主键(id
); -- 转储表的AUTO_INCREMENT -- 表posts
AUTO_INCREMENT 更改表posts
修改id
INT(11) 不空 AUTO_INCREMENT, AUTO_INCREMENT=92; 犯;
我需要根据任何帖子中各自的评论对回复进行分组,其中包含评论和回复。 即评论下的回复。
尽管这种方法可能很昂贵,但使用您的想法,这是您在选择每个注释并浏览它们以打印它们后应该采取的步骤
- 在每个循环期间,获取注释 ID 并在回复表中查询与该 ID 匹配的回复
-
获得回复后,创建一个内部循环并输出结果
$comments = mysqli_query($conn, "SELECT * FROM comments WHERE post_id = $postId)"; while($comment = mysqli_fetch_assoc($comments)){ echo $comment['message']; $replies = mysqli_query($conn, "SELECT * FROM replies WHERE commentId = $comment['comments_id']); while($reply = mysqli_fetch_assoc($replies)){ echo $reply['message']; } }