Php 层次评论系统
我想使用discus/reddit/类似的评论系统,我的评论数据库中有一个“id_answer”(通过defaut设置为0)字段,当用户回答另一个评论时,该字段是父评论的“id” 我有一个数组中线程的注释,但我不知道如何过滤每个循环以获得如下内容: 注释级别1($array[id\u answer]为0) -------注释级别2($array[id\u answer]是一级注释的id\u) ---------------评论3级Php 层次评论系统,php,mysql,multidimensional-array,nested-sets,Php,Mysql,Multidimensional Array,Nested Sets,我想使用discus/reddit/类似的评论系统,我的评论数据库中有一个“id_answer”(通过defaut设置为0)字段,当用户回答另一个评论时,该字段是父评论的“id” 我有一个数组中线程的注释,但我不知道如何过滤每个循环以获得如下内容: 注释级别1($array[id\u answer]为0) -------注释级别2($array[id\u answer]是一级注释的id\u) ---------------评论3级 在数据库中使用三个字段 “id”,每个注释都是唯一的 “父注释
在数据库中使用三个字段
- “id”,每个注释都是唯一的
- “父注释id”,它是0(顶级注释)或父注释的“id”
- “thread_id”,这是您评论的内容的id
SELECT id, parent FROM comments WHERE thread_id = 123
然后,您应该预解析数组以将子注释提供给它们的父级,并使用递归显示来显示每个注释及其子列表
例如:
// getting the comments from mysql, I'm obviously not bothering
// to check the return value, but in your code you should do it
$result = mysqli_query("SELECT id, parent FROM comments WHERE thread_id = 123");
$comments = array();
while ($row = mysqli_fetch_array($result)) {
$row['childs'] = array();
$comments[$row['id']] = $row;
}
// This is the array you get after your mysql query
// Order is non important, I put the parents first here simply to make it clearer.
/*
$comments = array(
// some top level (parent == 0)
1 => array('id' => 1, 'parent' => 0, 'childs' => array()),
5 => array('id' => 5, 'parent' => 0, 'childs' => array()),
2 => array('id' => 2, 'parent' => 0, 'childs' => array()),
10 => array('id' => 10, 'parent' => 0, 'childs' => array()),
// and some childs
3 => array('id' => 3, 'parent' => 1, 'childs' => array()),
6 => array('id' => 6, 'parent' => 2, 'childs' => array()),
4 => array('id' => 4, 'parent' => 2, 'childs' => array()),
7 => array('id' => 7, 'parent' => 3, 'childs' => array()),
8 => array('id' => 8, 'parent' => 7, 'childs' => array()),
9 => array('id' => 9, 'parent' => 6, 'childs' => array()),
);
*/
// now loop your comments list, and everytime you find a child, push it
// into its parent
foreach ($comments as $k => &$v) {
if ($v['parent'] != 0) {
$comments[$v['parent']]['childs'][] =& $v;
}
}
unset($v);
// delete the childs comments from the top level
foreach ($comments as $k => $v) {
if ($v['parent'] != 0) {
unset($comments[$k]);
}
}
// now we display the comments list, this is a basic recursive function
function display_comments(array $comments, $level = 0) {
foreach ($comments as $info) {
echo str_repeat('-', $level + 1).' comment '.$info['id']."\n";
if (!empty($info['childs'])) {
display_comments($info['childs'], $level + 1);
}
}
}
display_comments($comments);
这将得出以下结果:
- comment 1
-- comment 3
--- comment 7
---- comment 8
- comment 5
- comment 2
-- comment 6
--- comment 9
-- comment 4
- comment 10
我将订单交给您,因为这不会带来任何问题。不,我需要在数组中选择id\u answer=0,然后为每个注释再次选择id\u answer=id\u comment,我知道如何使用sql\u查询,但我不希望每个注释都执行一个查询。仅供参考:您可以(但不能,只是一个提示)将前两个
foreach
缩减为一个:(子键0
您的代码中有childs
。@hakre:谢谢,我从我的示例数组开始,忘了他在获取行时可以将注释id作为数组的键,编辑并简化了很多。但是,我仍然需要在之后的第二个foreach中取消设置,因为我不能保证该行在他的排序之后的顺序。@Tahola:由于hakre的评论,我在我的回答中编辑了代码,现在的代码应该更容易理解read@Lepidosteus:您甚至可以取消设置值的别名,请参见链接示例,这是一个很好的移动。@Lepidosteus:只要ID的排序为0-n,并且ParentID与second相同,这应该就可以了。否则,包含该元素的引用数组和$map[$id]
可以对任何顺序执行该操作。我想你也是。