public function get_children($id)
{
$query = $this->db->query("SELECT
@last_id := id AS id,
parent_id,
FIO,
@depth := IF(parent_id = ".$id.", 1, @depth + 1) AS depth,
@first := IF(id = ".$id.", 'first', null) AS first
FROM
users
FORCE INDEX (index_parent_id, PRIMARY, index_both),
(SELECT @last_id := ".$id.", @depth := -1) vars
WHERE id = ".$id." OR parent_id = @last_id OR parent_id = ".$id."
");
return $query;
}
如何使用相邻模型列表和此查询增加子节点的级别深度,现在它给了我根父节点(由函数中的 id 选择)及其子节点,但不是更深,有什么想法吗?
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);
然后我将它呈现在视图文件中......请帮助或任何关于更改或查看以增加 mysql 查询深度的建议。