I have the following issue : imaging an adjacency list, that’s been walked over with a recursion
coming out of a sql like this
SELECT * FROM pages as ps ORDER BY COALESCE(child_of,page_id), page_id LIMIT x,y
public static function tree(&$arr, $id = NULL) {
$result = array();
foreach ($arr as $a) {
if ($id == $a['child_of']) {
$a ['children'] = self::tree($arr, $a['page_id']);
$result[] = $a;
}
}
return $result;
}
So far, so good – with another “flattener” I am getting where I need to be. Now , here is the trick ,
this works on “paginated” results, and what possibly can happen ( and it does ) is that the parent can be in one subset and the child in a different subset. With the recursion above is obvious that
the child won’t make it to the tree with a missing parent.
Any ideas on how can i solve that?
Help is much appreciated.
Hierarchical data in relational tables, don’t we all love it?
With your current database layout, you can only solve your problem by either always fetching all nodes, or doing as many
JOINSas you have nesting levels, sort everything properly (your way of sorting only makes this fundamental problem, that you have, a little less important).Before you ask, No, you should not do this.
The other method you have, is choose an entirely different model to create your hierarchy:
See slide 48 et seq. here.