Answer the question
In order to leave comments, you need to log in
Tree comments
What is the best algorithm to organize tree-like comments with nesting level 2?
Is it reasonable to use tree building algorithms here?
Answer the question
In order to leave comments, you need to log in
do not need any trees and parentID. because of them, you can run into JOINs or the problem of (n + 1) queries. everything is simpler:
1) select the attribute by which you sort the comments in the topic. for example this is created_at.
2) store created_at and parent_created_at for all comments. for root, set parent_created_at to created_at.
3) Selection ORDER parent_created_at DESC, created_at DESC receives comments in the right order
4) when rendering by checking parent_created_at is equal to created_at - without indent, not equal - with indent
5) ?????
6) PROFIT comment tree with max. nesting level equal to two
If we are talking about SQL, then there is one very good solution for sorting tree-like comments into one SQL query without LEFT JOIN, by the way, which, using AJAX, allows you to reload separate branches starting with any comment in the chain.
Suppose adding comments to a topic looks like this:
$arr = array(
array('id'=>1, 'pid'=>0, 'name'=>'Comment 1'),
array('id'=>2, 'pid'=>1, 'name'=>'Comment 1.1'),
array('id'=>3, 'pid'=>1, 'name'=>'Comment 1.2'),
array('id'=>6, 'pid'=>1, 'name'=>'Comment 1.3'),
array('id'=>4, 'pid'=>2, 'name'=>'Comment 1.1.1'),
array('id'=>5, 'pid'=>2, 'name'=>'Comment 1.1.2'),
array('id'=>7, 'pid'=>2, 'name'=>'Comment 1.1.3'),
array('id'=>8, 'pid'=>0, 'name'=>'Comment 2'),
array('id'=>12, 'pid'=>8, 'name'=>'Comment 2.1'),
array('id'=>17, 'pid'=>8, 'name'=>'Comment 2.2'),
array('id'=>13, 'pid'=>12, 'name'=>'Comment 2.1.1'),
array('id'=>16, 'pid'=>13, 'name'=>'Comment 2.1.1.1'),
array('id'=>9, 'pid'=>0, 'name'=>'Comment 3'),
array('id'=>14, 'pid'=>9, 'name'=>'Comment 3.1'),
array('id'=>15, 'pid'=>14, 'name'=>'Comment 3.1.1'),
array('id'=>10, 'pid'=>0, 'name'=>'Comment 4'),
array('id'=>11, 'pid'=>0, 'name'=>'Comment 5'),
);
$arr = array(
array('id'=>1, 'pid'=>0, 'name'=>'Comment 1'),
array('id'=>8, 'pid'=>0, 'name'=>'Comment 2'),
array('id'=>9, 'pid'=>0, 'name'=>'Comment 3'),
array('id'=>10, 'pid'=>0, 'name'=>'Comment 4'),
array('id'=>11, 'pid'=>0, 'name'=>'Comment 5'),
array('id'=>2, 'pid'=>1, 'name'=>'Comment 1.1'),
array('id'=>3, 'pid'=>1, 'name'=>'Comment 1.2'),
array('id'=>6, 'pid'=>1, 'name'=>'Comment 1.3'),
array('id'=>4, 'pid'=>2, 'name'=>'Comment 1.1.1'),
array('id'=>5, 'pid'=>2, 'name'=>'Comment 1.1.2'),
array('id'=>7, 'pid'=>2, 'name'=>'Comment 1.1.3'),
array('id'=>12, 'pid'=>8, 'name'=>'Comment 2.1'),
array('id'=>17, 'pid'=>8, 'name'=>'Comment 2.2'),
array('id'=>14, 'pid'=>9, 'name'=>'Comment 3.1'),
array('id'=>13, 'pid'=>12, 'name'=>'Comment 2.1.1'),
array('id'=>16, 'pid'=>13, 'name'=>'Comment 2.1.1.1'),
array('id'=>15, 'pid'=>14, 'name'=>'Comment 3.1.1'),
);
for ($i = 0, $c = count($arr); $i < $c; $i++)
{
$new_arr[$arr[$i]['pid']][] = $arr[$i];
}
function my_sort($data, $parent = 0, $level = 0)
{
$arr = $data[$parent];
for($i = 0; $i < count($arr); $i++)
{
echo '<div style="padding-left:' . $level . 'px;">';
echo $arr[$i]['name'];
if(isset($data[$arr[$i]['id']])) my_sort($data, $arr[$i]['id'], 20);
echo '</div>';
}
}
Well, if the nesting is 2 and will not increase and the sql-base will be used, then you can use the id / parent_id bundle, you can not use the path algorithms and nested set :) And if not sql, but say nosql (for example, mongodb), then there even easier.
In this case, it is enough to keep only the parent of the comment, and then ask if there are comments with such a parent,
or
Do something like this:
auto-increment on id = 100, ty. 100, 200, 300, etc...
table:
id, level, comment
100, 1, "comment 1"
insert answers to the comment into the table from +1 to id.
101, 2, "comment to comment 1"
Why not make nesting unlimited, but display the same for all levels above the first one?
Most importantly, when you make a tree, make a discrete gradient in front of the comment so that you can easily see what level the comment is.
Because on Habré, for example, when a comment thread grows, sometimes you don’t understand who answers to whom.
phpclub.ru/detail/article/db_tree
is still worth studying nestedtree
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question