0
votes

I'm trying to make a comment system with nested comments. But, I only want indentation for the whole reply block so I don't end up with ex. 2 indentations if there is a reply to the reply.

So I want something like this

Parent
  Child
  Child
  Child
Parent

I got an array with all comments looking like this (removed things like date, name etc)

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

How can I sort the array so the child's are listed after their parent?

1
Unclear question and no effort. - Andreas

1 Answers

0
votes

I have made a solution that works for me now, if anyone is interested here is the code. This can probably be shortened down.

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

        }

    }

?>