如何根据博客文章中的评论对回复进行排序



我的问题是如何根据每个帖子中的评论对回复进行分组。我是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(idint(11) 不为空,date日期时间默认值空,name瓦尔查尔(255) 不为空,username瓦尔查(255) 不为空,post_idint(11) 不为空,email瓦尔查尔(255) 不为空,website瓦尔查尔(255) 不为空,image瓦尔查尔(255) 不为空,comment文本不为空,status瓦尔查尔(255) 不为空,posts_idint(11) 不为空 ) 引擎=InnoDB 默认字符集=拉丁语1;

插入comments(iddatenameusernamepost_idemailwebsiteimagecommentstatusposts_id)值 (136, '2019-02-08 01:16:17', 'Val Amasiatu

', 'compus', 91, 'valamasiatu@gmail.com', 'avidscholars.com.ng', 'Unknown_Member.jpg', 'Keep it up', 'approval', 91),

-- 表comments的索引

更改表comments添加主键 (id), 使用BTREE添加密钥comments and posts link(posts_id); -- 转储表的AUTO_INCREMENT

-- 表commentsAUTO_INCREMENT

更改表comments修改idINT(11) 不空 AUTO_INCREMENT, AUTO_INCREMENT=155; -- 转储表的约束

-- 表comments的约束

更改表comments在更新级联上删除级联Foreign key to posts table外键(posts_id)引用posts(id)添加约束; 犯;

包含示例数据的回复表结构

创建表replies(idint(11) 不为空,date日期时间不为空,name瓦尔查尔(255) 不为空,username瓦尔查尔(255) 不为空,email瓦尔查尔(255) 不为空,image瓦尔查尔(255) 不为空,comment文本不为空,status瓦尔查尔(255) 不为空,comments_idint(11) 不为空 ) 引擎=InnoDB 默认字符集=拉丁语1; -- 转储表replies的数据 插入replies(iddatenameusernameemailimagecommentstatuscomments_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修改idINT(11) 不空 AUTO_INCREMENT, AUTO_INCREMENT=168; -- 转储表的约束 -- 表replies的约束 更改表replies在更新级联上删除级联Foreign key to replies table外键(comments_id)引用comments(id)添加约束; 犯;

帖子表的结构和示例数据

创建表posts(idint(11) 不为空,postDate日期时间不为空,title瓦尔查尔(255) 不为空,author瓦尔查尔(255) 不为空,author_image瓦尔查尔(255) 不为空,image瓦尔查尔(255) 不为空,categories瓦尔查尔(255) 不为空,tags瓦尔查尔(255) 不为空,post_data文本不为空,viewsint(11) 不为空,status瓦尔查尔(255) 不为空 ) 引擎=InnoDB 默认字符集=拉丁语1; -- 转储表posts的数据 插入posts(idpostDatetitleauthorauthor_imageimagecategoriestagspost_dataviewsstatus)值 (42, '2019-02-03 11:21:22', '什么是Lorem Ipsum?', '1234', 'AvePoint.jpg', 'admin-balance-blur-272978.jpg', '教程', '我的,我的,我的', '

但是我无法做的是找到并将这个丢失的结束标签div放在它应该在的地方。

', 8, '发布'),

-- 表posts的索引

更改表posts添加主键(id); -- 转储表的AUTO_INCREMENT -- 表postsAUTO_INCREMENT 更改表posts修改idINT(11) 不空 AUTO_INCREMENT, AUTO_INCREMENT=92; 犯;

我需要根据任何帖子中各自的评论对回复进行分组,其中包含评论和回复。 即评论下的回复。

尽管这种方法可能很昂贵,但使用您的想法,这是您在选择每个注释并浏览它们以打印它们后应该采取的步骤

  1. 在每个循环期间,获取注释 ID 并在回复表中查询与该 ID 匹配的回复
  2. 获得回复后,创建一个内部循环并输出结果

    $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']; } }

最新更新