Camen Design Forum

Comment Threading

append delete Adam S.

Great post. I have played with threading in many ways, some less successful and some more. The OSnews implementation you've screenshotted, as you know, is based primarily on css and margin/padding. I've used other methods, and tried to share them, but beware, this is old and problems are solved differently now:…

That said, your solutions are always elegant and I love when you share code like this.

Reply RSS


#1. J to the ohann

This post was deleted by its owner

append delete #2. J to the ohann

But if you're retrieving the comments from a database anyway, and especially when there might be many of them and you neither want to have to sort them nor display all of them, you kinda owe it to yourself to look into n-level trees...

Here's my function to rebuild the comment tree, more or less verbatim from some tutorial: (do note though that one doesn't actually need to rebuild the whole tree when inserting/deleting nodes, but I'll optimize that when it becomes an issue)

function rebuild_comment_tree($table, $profile_id, $target_id, $parent_id, $left, $level)
$right = $left + 1;
$r = fma($db->query('SELECT id FROM '.$table.' WHERE profile_id='.$profile_id.' AND target_id='.$target_id.' AND parent_id='.$parent_id . ' ORDER BY time_created ASC'));
foreach ($r as $k => $v)
$right = function rebuild_comment_tree($table, $profile_id, $target_id, $v['id'], $right, $level + 1);
if ($parent_id != 0)
$db->query('UPDATE '.$table.' SET nleft='.$left.', nright='.$right.', nlevel = '.$level.' WHERE profile_id='.$profile_id.' AND target_id='.$target_id.' AND id="'.$parent_id.'";');
return $right+1;

I'm not posting this here because it's readable, but to make a point: it's hardly a lot of code. (it's just a lot of database requests when updating something *ahem*)

Retrieving the comments then requires zero effort, you can do "ORDER BY nleft" to get them as a tree (and unless your webserver is very different from mine, that is a *lot* faster), using the "nlevel" value to indent it appropriately.. done. And if you use it for other stuff besides comments, it's very, very easy to get children, descendants or parents of a node without having to load all nodes first.

See this or other articles for more details:…

append delete #3. Kroc

@Adam S. Thanks, indenting blocks so that the comments _appear_ nested, even though they wouldn’t be in the HTML was one of the solutions I was considering. It’s certainly a million times easier, but I am not one to give in just because something seems like it can’t be done :P I needed proper nesting for the purposes of using particular CSS3, and parallel blocks wouldn’t cut it.

The other thing I am averse too is scatter-code -- i.e. when you end up with loads of ancillary functions spread across multiple files and it’s painful for anybody to learn; I did begin as a BASIC programmer, so it’s only natural to me for code to read from top to bottom in a straight fashion :P


(Leave this as-is, it’s a trap!)

There is no need to “register”, just enter the same name + password of your choice every time.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn