PHP评论嵌套功能实现方法,核心代码摘自WordPress

来自:互联网
时间:2018-08-05
阅读:
<?php
$comments = array (
        array (
                'id' => '3',
                'parent' => '0'
        ),
        array (
                'id' => '9',
                'parent' => '0'
        ),
        array (
                'id' => '1',
                'parent' => '3'
        ),
        array (
                'id' => '2',
                'parent' => '3'
        ),
        array (
                'id' => '5',
                'parent' => '1'
        ),
        array (
                'id' => '7',
                'parent' => '1'
        )
);
function html5_comment($comment) {
        echo '<li>';
        echo 'id:', $comment['id'], ' parent:', $comment['parent'];
}
function start_el(& $output, $comment) {
        ob_start();
        html5_comment($comment);
        $output .= ob_get_clean();
}
function end_el(& $output) {
        $output .= "</li><!-- #comment-## -->n";
}
function start_lvl(& $output) {
        $output .= '<ol class="children">' . "n";
}
function end_lvl(& $output) {
        $output .= "</ol><!-- .children -->n";
}
function display_element($e, & $children_elements, $max_depth, $depth, & $output) {
        $id = $e['id'];
        start_el($output, $e); //当前评论的开始代码
        if ($max_depth > $depth +1 && isset ($children_elements[$id])) { //如果没超过最大层,并且存在子元素数组
                foreach ($children_elements[$id] as $child) {
                        if (!isset ($newlevel)) { //第一次循环没设置变量$newlevel,所以把$newlevel设为true,并且开始子元素的开始代码;第二次及之后的循环,已经设置了$newlevel,就不会再添加子元素的开始代码。因为同一批循环时兄弟元素,所以只需要一个子元素开始代码,循环内容为并列关系。
                                $newlevel = true;
                                start_lvl($output);
                        }
                        display_element_template($child, $children_elements, $max_depth, $depth +1, $output); //$child作为参数,继续去寻找下级元素
                }
                unset ($children_elements[$id]); //用完释放变量,以后就不会重复判断该值了,递归后继续判断剩下的子元素
        }
        if (isset ($newlevel) && $newlevel) { //如果前面找到了子元素,这里就要执行子元素的结束代码
                end_lvl($output);
        }
        end_el($output); //当前评论的结束代码
}
function display_element_template($e, & $children_elements, $max_depth, $depth, & $output) {
        $id = $e['id'];
        display_element($e, $children_elements, $max_depth, $depth, $output);
        if ($max_depth <= $depth +1 && isset ($children_elements[$id])) { //如果超出最大层级,并且子元素存在的话,以$child为参数继续往下找
                foreach ($children_elements[$id] as $child) {
                        display_element_template($child, $children_elements, $max_depth, $depth, $output);
                }
                unset ($children_elements[$id]); //用完释放变量
        }
}
function comments_list($comments) {
        $top_level_elements = array ();
        $children_elements = array ();
        foreach ($comments as $e) {
                if (0 == $e['parent']) {
                        $top_level_elements[] = $e;
                } else {
                        $children_elements[$e['parent']][] = $e;
                }
        }
        $output = '';
        foreach ($top_level_elements as $e) {
                display_element_template($e, $children_elements, 2, 0, $output);
        }
        //var_dump($children_elements);//由于每次用完$children_elements后都会释放变量,所以到最后$children_elements为空数组
        return $output;
}
echo '<ol class="comment-list">', comments_list($comments), '</ol>';
返回顶部
顶部