获取树评论

时间:2016-10-08 18:38:10

标签: php codeigniter tree

无法从数据库中获取正常的评论树:

my table in database

模型中的函数来获取数据(我使用Codeigniter)

public function get_test()
    {
        $query = $this->db->get('test');
        $dataset = $query->result_array();
        foreach($dataset as &$row) {
            if (!$row['parent_id']) {
                $tree[$row['id']] = &$row;
            } else {
                $tree[$row['parent_id']]['children'][$row['id']] = &$row;

            }
        }
        return $tree;
    }

<? echo '<pre>';
    print_r($test);
    echo '</pre>'; ?>

这就是我得到的:

Array
(
    [1] => Array
        (
            [id] => 1
            [parent_id] => 0
            [name] => Маша
            [comment] => Привет всем
            [children] => Array
                (
                    [2] => Array
                        (
                            [id] => 2
                            [parent_id] => 1
                            [name] => Саша
                            [comment] => И тебе првиет
                        )

                )

        )

    [3] => Array
        (
            [id] => 3
            [parent_id] => 0
            [name] => Даша
            [comment] => Ауау
            [children] => Array
                (
                    [4] => Array
                        (
                            [id] => 4
                            [parent_id] => 3
                            [name] => Паша
                            [comment] => Читай
                        )

                )

        )

    [2] => Array
        (
            [children] => Array
                (
                    [5] => Array
                        (
                            [id] => 5
                            [parent_id] => 2
                            [name] => Петя
                            [comment] => Еще привет
                        )

                )

        )

)

最后一个数组它必须是对第一个数组中注释的响应....问题是什么?我找不到错误。

1 个答案:

答案 0 :(得分:0)

可以使用phpmyadmin再次检查您的数据库,因为我从打印数组中看到<form action="https://maker.ifttt.com/trigger/web_button_pressed/with/key/" method="post"> First name:<br> <input type="text" name="value1"><br> Last name:<br> <input type="text" name="value2"> <!--Send E-Mail!--> <button class="my-button-css" name="sendmailbutton" value="send email">Send E-Mail</button> </form> 的评论有[id] => 5。如果您希望将其显示为[parent_id] => 2评论的子级,则必须向其提供[id] => 1