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.'";');
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: