Jump to content

Getting a Comment Tree From an Array of Comments


proggR

Recommended Posts

I'm working on adding a threaded comment system to a website. The comments will be in reply to posts which are part of a post table. The comment table contains an auto incremented ID, a ThreadID that references the post its replying to and a CommentID that references which comment its replying to if it is a reply to a comment and is null if its a reply directly to the post. It also contains the comment obviously. I can get all the comments for a post and push each comment object into an array and return it to be displayed.

What I'm stuck on though is having the comments nested. Any idea's I've came up with fall short of being able to be n level trees.

I'm sure the answer is something to do with recursion but I can't think of how it would be implemented in this case.

Any help or pointers would be great. I'm reading around but I thought I'd stop in here and ask as well.

Thanks in advance.

Link to comment
Share on other sites

I do have the date recorded but it's just to display. They should be displayed based on the order of the root comment, which would be chronological I suppose. But the children's datestamp shouldn't reorder the branch of comments. It will just be a simple top down, first to last.

Link to comment
Share on other sites

So I think I figured it out last night in a dream  :shrug:. I don't know if it will be slow or not though so if anyone could point out any tweaks that'd be great.

 

From the file that will display the data I would write:

$comments;//comments is the array of comment objects
$ui->printReplies($comments,null);//$ui is an object of type UI that handles the GUI elements, null is passed because its the top level

 

Then inside the UI class I would have the print replies function look like this:

function printReplies(&$comments, $id){
foreach($comments as $comment){
  if($comment->commentID == $id){
    $this->comment($comment);//prints the comment, this refers to the UI class
    $this->printReplies($comments, $comment->id); //recursively calls the method, repeating for that comment
    echo '</div>'; //the comment div stays open until there are no more children
  }
}
}

 

 

Is there any tweaks I could make to make this faster? Is there a way to remove the comment from the array from inside this if statement to make the array a bit smaller each time? Also my passing by reference syntax may be wrong.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.