Recursive function for comments and responses of a PHP application

I am having difficulty conceptualizing a recursive function to add responses to comments, responses to answers, answers to answers to answers, etc.

This is my comment table:

enter image description here

Which SHOULD look like the one shown when rendering:

enter image description here

At its core, I can display every comment related to article_id (with the exception of those NOT NULL, of course):

$comments = $commentClass->fetch_article_comments($article_id);

foreach($comments as $comment) {

        $comment_id = $comment['comment_id'];   
        $member_id = $comment['member_id'];
        $comment_text = $comment['comment_text'];
        $comment_timestamp = timeAgo($comment['comment_timestamp']);  //get time ago

        //render comment using above variables
    }

Now I assume that I need to add a recursive function at the end of the above statement foreach, but I still need to come up with something remotely successful, adding comment replies and answers to each answer.

- , , . . stackoverflow, -, , .

, , , php.

.



, , . , ?

php:

function display_comments($article_id, $parent_id=0, $level=0) {
    global $comment;
    global $member;
    global $signed_in;
    global $username;

    $comments = $comment->fetch_article_comments($article_id, $parent_id);

    foreach($comments as $data) {
        $comment_id = $data['comment_id'];   
        $c_member_id = $data['member_id'];
        $comment_text = $data['comment_text'];
        $comment_timestamp = timeAgo($data['comment_timestamp']);  //get time ago

        $member_data = $member->fetch_member_data($c_member_id);
        $c_member_username = $member_data['member_username'];
        $c_member_avatar = $member_data['member_avatar'];

                //render HTML here

                $parent = $data['comment_parent'];
                display_comments($article_id, $parent, $level+1);

    }//end foreach

}//end display_comments()

PDO:

public function fetch_article_comments($article_id, $parent_id) { //$page = (int)(!isset($_GET['page'])) ? 0 : $_GET['page'];

    if ($parent_id > 0) {
        $parent_id = "= $parent_id";
    } else {
        $parent_id = "IS NULL";
    }

    $query = $this->db->prepare("SELECT * FROM `comments2` WHERE `article_id` = $article_id AND `comment_parent` $parent_id ORDER BY `comment_timestamp` DESC");

    try{
        $query->execute();
        $comments = $query->fetchAll();                                     //returns an array
        $query->closeCursor();

        return $comments;

    } catch(PDOException $e){
        die($e->getMessage());
    }
}
+4
2

:

$comments = $commentClass->fetch_article_comments($article_id);

, somwhere SQL, SELECT ... WHERE comments.article_id=$article_id. - -

$comments = $commentClass->fetch_article_comments($article_id, $parent_id);

SQL, SELECT ... WHERE comments.article_id=$article_id AND comments.comment_parent ($parent_id>0)?"=$parent_id":"IS NULL"

PHP:

function display_comments ($article_id, $parent_id=0, $level=0) {
  $comments = $commentClass->fetch_article_comments($article_id, $parent_id);
  foreach($comments as $comment) {
        $comment_id = $comment['comment_id'];   
        $member_id = $comment['member_id'];
        $comment_text = $comment['comment_text'];
        $comment_timestamp = timeAgo($comment['comment_timestamp']);  //get time ago

        //render comment using above variables
        //Use $level to render the intendation level

        //Recurse
        display_comments ($article_id, $comment_id, $level+1);
    }
}

, , display_comments ($article_id);

+2

- :

function getCommentsForParent($parent="") {
   echo "<div class=\"comment\">";
   $db = get("select your db logics where parent = $parent");
   foreach ($db as $comment) {
      echo "print your comment in a box and make a div around the hole comments"
      getCommentsForParent($comment['parent_id']);
   }
   echo "</div>";
}

getCommentsForParent()

- :

function getCommentForParent($parent="") {
   global $commentsClass, $article_id;
   $comments = $commentClass->fetch_article_comments($article_id,$parent);
   foreach($comments as $comment) {
        $comment_id = $comment['comment_id'];   
        $member_id = $comment['member_id'];
        $comment_text = $comment['comment_text'];
        $comment_timestamp = timeAgo($comment['comment_timestamp']);  //get time ago
        getCommentForParent($comment['parent']);
    }
}

, article_comments. $parent. , , $parent = "", .

getCommentsForParent()

. , , .

+1

All Articles