Can't get normal tree of comments from database:
Function in model to get data (I use Codeigniter)
public function get_test()
{
$query = $this->db->get('test');
$dataset = $query->result_array();
foreach($dataset as &$row) {
if (!$row['parent_id']) {
$tree[$row['id']] = &$row;
} else {
$tree[$row['parent_id']]['children'][$row['id']] = &$row;
}
}
return $tree;
}
<? echo '<pre>';
print_r($test);
echo '</pre>'; ?>
And this is what I get:
Array
(
[1] => Array
(
[id] => 1
[parent_id] => 0
[name] => Маша
[comment] => Привет всем
[children] => Array
(
[2] => Array
(
[id] => 2
[parent_id] => 1
[name] => Саша
[comment] => И тебе првиет
)
)
)
[3] => Array
(
[id] => 3
[parent_id] => 0
[name] => Даша
[comment] => Ауау
[children] => Array
(
[4] => Array
(
[id] => 4
[parent_id] => 3
[name] => Паша
[comment] => Читай
)
)
)
[2] => Array
(
[children] => Array
(
[5] => Array
(
[id] => 5
[parent_id] => 2
[name] => Петя
[comment] => Еще привет
)
)
)
)
The last array It must be a response to a comment in the first array....What is the problem? I can't find a mistake.
Check again your database maybe by using phpmyadmin because as i saw from the printed array the comment with [id] => 5
has [parent_id] => 2
. If you want to display it as child of the comment with [id] => 1
you have to give him the [parent_id] => 1