Welcome Guest, Not a member yet? Register   Sign In
How do threaded comments work? [SOLVED]
#1

[eluser]KeyStroke[/eluser]
Hi,

I'm trying to make a simple threaded (aka: nested) comments system for the posts on my web site.

I've made a table for the comments where each comment has an "id" and a "parent_id" to link it to its parent.

The problem is that I don't know how to code the comments in the view so they're displayed properly. Any ideas?
#2

[eluser]Thorpe Obazee[/eluser]
You'd be looping within loops I guess to get everything.
#3

[eluser]KeyStroke[/eluser]
Well, of course, but I'm asking about HOW to do the loops (properly).

Here's a sample code to show where I'm stuck at:
Code:
foreach($comments as $comment)
{
      echo $comment['title'];
      echo $comment['body'];
      
      // what should I do here to display comment replies (if any)?
}
#4

[eluser]KeyStroke[/eluser]
Ok I've a solution. It's pretty messy but it worked. I'd appreciate it though if someone shares something about this.
#5

[eluser]xwero[/eluser]
Adding a level field would save you a lot of head aches. The level gets set when a comment gets added with a parent_id and then you check the id's until you find id which has level 0.

You can use the level as a css class (level0, level1, ...) or you can include the comments that have a higher lever than 0.
The down side of level as css class is that css is static. You could replace the css class with a level generated style attribute.

If you use indentation there is a possibility your lay-out breaks if there is a whole tree of replying on comments.

Another way would be to have a flat list and you just link to the parent comment
Code:
<a href="topic/1/comments/#5">My response on this comment</a> : reponse
This won't break your lay-out.
#6

[eluser]KeyStroke[/eluser]
Thanks xwero for the suggestion. My question was more about the loops rather than the looks. I got something like this now:
Code:
for($comments as $comment)
{
   // if a comment has a parent
   if($comment['parent_id'] != NULL)
   {
       // skip it since it doesn't belong to this level
       continue;
   }

   echo $comment['body'];

   for($comments as $sub_comment)
   {
      // if you find that this comment is a parent
      if($comment['id'] == $sub_comment['parent_id'])
      {  
         // print the child underneath it
         echo $sub_comment['body'];
      }
   }
}

It looks kinda hackish to me. I can't imagine adding even more "foreach" levels if I needed more levels in my comments.

Maybe someone has a better version for a code like this?
#7

[eluser]xwero[/eluser]
You have to use a recursive function for this. Here is a long article to introduce yourself with recursive functions.
#8

[eluser]m4rw3r[/eluser]
Maybe using nested sets to store them?
It eliminates a lot of the loops.

My lib has a get_descendants() function which can return them with a level column, and also a tree2array() function.
If you want some help regarding nested sets trees, I can help you.




Theme © iAndrew 2016 - Forum software by © MyBB