【问题标题】:Nested comments, sort array to list child after their parent嵌套评论,排序数组以在其父级之后列出子级
【发布时间】:2018-02-07 17:09:05
【问题描述】:

我正在尝试使用嵌套的 cmets 创建一个评论系统。但是,我只想要整个回复块的缩进,所以我不会以 ex 结尾。如果有回复,则缩进 2 个。

所以我想要这样的东西

Parent
  Child
  Child
  Child
Parent

我得到了一个所有 cmets 看起来像这样的数组(删除了日期、名称等内容)

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 )
)

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

【问题讨论】:

  • 不清楚的问题,没有努力。

标签: php


【解决方案1】:

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

<?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;

        }

    }

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 2021-12-23
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多