在数据库中的分层数据结构中查询并显示结果


querying in hierarchical data-structure in database and displaying results

我想使用PHP-MySql在我的网站上实现高级评论系统。为此,我最终选择了 3 级评论-回复系统。好吧,出于这个目的,我遇到了这篇文章来实现SQL数据库的数据结构。

我计划使用嵌套集 mdoel 来实现我想使用的功能。评论的结构是这样的——

<ul>
    <li>Parent comment</li>
        <ul>
            <li>First reply of parent comment</li>
            <ul>
                <li>reply of the previous reply</li>
                   <ul>
                       <li>reply of the previous reply</li>
                       <li>another reply of the previous reply</li>
                   </ul>
                <li>another reply of the previous comment</li>
             </ul>
             <li>second reply of the parent comment</li>
        </ul>
</ul>

对于这种类型的结构,我一直在使用 PHP 来显示唯一检测父项及其子项的查询(用于获取与每个注释关联的用户详细信息),并以如上所示的方式生成输出。有谁知道如何做到这一点。请帮帮我。

编辑:

我在SQL中有一个单独的用户表链接到注释表作为user.id=comment.id。因此,考虑到这一点,检测每条评论的用户活动的推荐方法是什么?我的意思是,例如,我想获取用户名和电子邮件以获取父评论 2 的子评论。哎呀能做到吗?

使用"查找节点深度"中的查询,此 PHP 代码将创建嵌套列表。

$cur_depth = -1;
while ($row = mysqli_fetch_assoc($query)) { // Loop through results of query
  if ($row['depth'] > $cur_depth) {
    echo "<ul>'n";
    $cur_depth = $row['depth'];
  }
  else while ($cur_depth > $row['depth']) {
    echo "</ul>'n";
    $cur_depth--;
  }
  echo "<li>" . $row['comment'] . "</li>'n";
}
while ($cur_depth > -1) {
  echo "</ul>'n";
  $cur_depth--;
}