我在论坛上搜索过,但没有得到权威的答案。我想以这样的方式实现嵌套注释结构:
<ul>
<li>This is the parent first comment!
<ul>
<li>This is the reply for the first parent comment!
<ul>
<li>This is a reply for the first reply of the parent comment!</li>
<li>This is a third reply for the first parent comment!</li>
</ul>
</li>
<li>This is another reply for first parent comment!</li>
</ul>
</li>
<li>This is gonna be parent second comment!
<ul>
<li>This is a reply for the second comment!</li>
</ul>
</li>
<li>This is fourth parent comment!</li>
</ul>
我的表的转储如下:
+----+------------------------------------------------------------+--------+
| id | text | parent |
+----+------------------------------------------------------------+--------+
| 1 | This is the parent first comment! | 0 |
| 2 | This is gonna be parent second comment! | 0 |
| 3 | This is the reply for the first parent comment! | 1 |
| 4 | This is another reply for first parent comment! | 1 |
| 5 | This is a reply for the first reply of the parent comment! | 3 |
| 6 | This is a reply for the second comment! | 2 |
| 7 | This is a third reply for the first parent comment! | 3 |
| 8 | This is fourth parent comment! | 0 |
+----+------------------------------------------------------------+--------+
我知道如何使用mysql_query()
和while()
循环。 PHP 和 MySQL 的初学者。请帮帮我。
最佳答案
有几种不同的方法可以在 MySQL 中存储层次结构数据。查看 Bill Karwin 的 presentation这演示了四个选项。
- 邻接列表
- 路径枚举
- 嵌套集
- 关闭表
您正在使用邻接列表模型来存储层次结构数据,但不幸的是,这是您可以选择用于查询子树的最难模型。
您的选择是:
- 更改为不同的型号。
- 将查询限制为 n 层深度。
- 使用存储过程进行递归查询。有关这方面的更多信息,请参阅 Quassnoi 的系列文章 - Hierarchical queries in MySQL .
关于php - PHP 和 MySQL 中的嵌套注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39872573/