proggR Posted December 2, 2010 Share Posted December 2, 2010 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 https://forums.phpfreaks.com/topic/220418-getting-a-comment-tree-from-an-array-of-comments/ Share on other sites More sharing options...
requinix Posted December 2, 2010 Share Posted December 2, 2010 Are you sorting by the comment date/time in chronological order? (Do you track that?) Link to comment https://forums.phpfreaks.com/topic/220418-getting-a-comment-tree-from-an-array-of-comments/#findComment-1142078 Share on other sites More sharing options...
proggR Posted December 2, 2010 Author Share Posted December 2, 2010 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 https://forums.phpfreaks.com/topic/220418-getting-a-comment-tree-from-an-array-of-comments/#findComment-1142097 Share on other sites More sharing options...
proggR Posted December 2, 2010 Author Share Posted December 2, 2010 So I think I figured it out last night in a dream . 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 https://forums.phpfreaks.com/topic/220418-getting-a-comment-tree-from-an-array-of-comments/#findComment-1142228 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.