我正在尝试创建一个评论系统,在这个系统中可以发表评论,人们可以在更深层次上回复评论(比如reddit)。我在一定程度上理解Child是如何引用Parent ID的,以便它可以显示在Parent ID下面,但您如何实际创建Parent ID,然后获得该ID供Child引用?
下面是我想要的一个例子:
Main Comment
Reply to Main Comment
Reply to Main Comment
Reply to Child Comment
Reply to Child Child Comment
Reply to Main Comment
Main Comment
我知道深度是:
0
1
1
2
3
1
0
但是,我如何分配父ID,使第一个主注释是AAA,第二个主注释为AAB,依此类推?
谢谢你的帮助!
Parent
- New Comment
- This comments parent is "New Comment"
- This comments parent is "New Comment"
- This Comments parent is "Parent"
父级"基本上"是其自身的一个注释,只是更多地访问格式。
首先,您要做的是设置表结构。在我实现的站点中,表有重要的字段,如name
、content
、email
等。此外,它还有一个parent_id
字段,可以引用表中的另一个注释。当注释处于顶级时,此字段为NULL
。
在我的PHP脚本中,它会查询所有具有parent_id = NULL
的注释,也就是说,只查询顶级的注释。然后,在循环浏览所有顶级注释时,我会查询parent_id
等于循环浏览的注释的id
的任何注释。然后我会用缩进来设计它的样式。
你的伪代码看起来像这样:
// Query for comments
while ($comment = mysqli_fetch_array($comments)) {
// Render top level comment
// Query for sub-level comments with parent_id of $comment
while ($subcomments = mysqli_fetch_array($subcomments)) {
// Render sub-level comment
}
}