嵌套注释,排序数组以列出其父项后的子项

时间:2018-02-07 17:09:06

标签: php

我正在尝试使用嵌套注释建立评论系统。但是,我只想缩小整个回复块,所以我不会以ex结尾。如果对回复有回复,则为2个缩进。

所以我想要这样的东西

Parent
  Child
  Child
  Child
Parent

我得到了一个包含所有评论的数组(删除日期,名称等内容)

Array ( 
    [0] => Array ( [ID] => 1 [BLOG_ID] => 3 [REPLY_TO] => 0 ) 
    [1] => Array ( [ID] => 2 [BLOG_ID] => 3 [REPLY_TO] => 1 )
    [2] => Array ( [ID] => 3 [BLOG_ID] => 3 [REPLY_TO] => 0 )
    [3] => Array ( [ID] => 4 [BLOG_ID] => 3 [REPLY_TO] => 2 )
    [4] => Array ( [ID] => 5 [BLOG_ID] => 3 [REPLY_TO] => 0 )
    [5] => Array ( [ID] => 6 [BLOG_ID] => 3 [REPLY_TO] => 1 )
    [6] => Array ( [ID] => 7 [BLOG_ID] => 3 [REPLY_TO] => 0 )
)

如何对数组进行排序,以便将子项列在其父项之后?

1 个答案:

答案 0 :(得分:0)

我已经找到了一个适合我的解决方案,如果有人对这里感兴趣的是代码。这可能会缩短。

<?php

    //-------------------------------------------------
    //  Get comments
    //-------------------------------------------------

    class Comments {

        private $comments;
        private $blog_id;

        function __construct() {

            $this->comments = [];

        }

        //-------------------------------------------------
        //  Count replys
        //-------------------------------------------------

        private function count_replys($id) {

            $db_conn2 = new Database;
            $reply_count = $db_conn2->count('COMMENTS', $sort = 'WHERE REPLY_TO = "'.$id.'"');

            return $reply_count;

        }

        //-------------------------------------------------
        //  Get reply childs
        //-------------------------------------------------

        private function get_reply_childs($id) {

            if($this->count_replys($id) > 0) {

                $db_conn = new Database;
                $stmt = $db_conn->connect->prepare("SELECT * FROM `COMMENTS` WHERE BLOG_ID=?  AND REPLY_TO=? ORDER BY `ID`");
                $stmt->bind_param("ii", $this->blog_id, $id);
                $stmt->execute();
                $result = $stmt->get_result();

                while ($row = $result->fetch_assoc()) {

                    array_push($this->comments, $row);

                    if($this->count_replys($row['ID']) > 0) {

                        $this->get_reply_childs($row['ID']);

                    }                  

                }

                $db_conn->free_close($result, $stmt);   

            }

        }

        //-------------------------------------------------
        //  Get root reply's
        //-------------------------------------------------

        private function get_root_replys($id) {

            if($this->count_replys($id) > 0) {

                $db_conn = new Database;
                $stmt = $db_conn->connect->prepare("SELECT * FROM `COMMENTS` WHERE BLOG_ID=?  AND REPLY_TO=? ORDER BY `ID`");
                $stmt->bind_param("ii", $this->blog_id, $id);
                $stmt->execute();
                $result = $stmt->get_result();

                while ($row = $result->fetch_assoc()) {

                    array_push($this->comments, $row);

                    $this->get_reply_childs($row['ID']);

                }

                $db_conn->free_close($result, $stmt);

            }

        }

        //-------------------------------------------------
        //  Get the comments
        //-------------------------------------------------

        function get_comments($blog_id) {

            $this->blog_id = $blog_id;

            $db_conn = new Database;
            $stmt = $db_conn->connect->prepare("SELECT * FROM `COMMENTS` WHERE BLOG_ID=?  AND REPLY_TO < 1 ORDER BY `ID`");
            $stmt->bind_param("i", $this->blog_id);
            $stmt->execute();
            $result = $stmt->get_result();

            while ($row = $result->fetch_assoc()) {

                array_push($this->comments, $row);

                $this->get_root_replys($row['ID']);

            }

            $db_conn->free_close($result, $stmt);

            return $this->comments;

        }

    }

?>