//here i am getting the parent_id of this root node
foreach($query->result() as $q){
if($q->first == 'first')
{
$parent_id = $q->parent_id;
break;
}
}
function print_list($array, $parent=0) {
print "<ul>";
foreach ($array as $row) {
if ($row->parent_id == $parent) {
print "<li>$row->FIO";
print_list($array, $row->id); # recurse
print "</li>";
} }
print "</ul>";
}
echo print_list($query->result(), $parent_id);
为什么它只呈现第一级深度?
How to build a tree in php having id, parent_id and depth variables - 此处的功能
的var_dump:
//var_dump($query->result());
array(4) {
[0]=>
object(stdClass)#18 (5) {
["id"]=>
string(3) "501"
["parent_id"]=>
string(1) "1"
["FIO"]=>
string(23) "Пётр Василий"
["depth"]=>
string(1) "0"
["first"]=>
string(5) "first"
}
[1]=>
object(stdClass)#19 (5) {
["id"]=>
string(3) "503"
["parent_id"]=>
string(3) "501"
["FIO"]=>
string(33) "Джумшут Джумшутов"
["depth"]=>
string(1) "1"
["first"]=>
NULL
}
[2]=>
object(stdClass)#20 (5) {
["id"]=>
string(3) "504"
["parent_id"]=>
string(3) "501"
["FIO"]=>
string(7) "dwadawd"
["depth"]=>
string(1) "1"
["first"]=>
NULL
}
[3]=>
object(stdClass)#21 (5) {
["id"]=>
string(3) "505"
["parent_id"]=>
string(3) "501"
["FIO"]=>
string(6) "vasekd"
["depth"]=>
string(1) "1"
["first"]=>
NULL
}
}
这是显示var_dump($ query-&gt; result());
的内容