getAll($sql);$g_db->close ();$list=roletree($roles,0);//Call function according to parent id"/> getAll($sql);$g_db->close ();$list=roletree($roles,0);//Call function according to parent id">

Home  >  Article  >  Backend Development  >  Introduction to Infinitus classification and sorting methods

Introduction to Infinitus classification and sorting methods

巴扎黑
巴扎黑Original
2017-07-18 17:27:561206browse

First the data table is similar to this, and then perform data query to obtain all the data

$sql   ="select * from ec_admin_role  where ar_state=1 order by ar_level";
$roles = $g_db->getAll($sql);
$g_db->close();
$list=roletree($roles,0);//调用函数按父级id进行排序
foreach($list as $k=>$v) {
$list[$k][&#39;ar_name&#39;] = str_repeat(&#39;----&#39;,$v[&#39;ar_level&#39;]).$v[&#39;ar_name&#39;].&#39;<br>&#39;;
}


//将角色经排序
function roletree($arr,$id=0) {
    $subs = array(); // 子孙数组
    foreach($arr as $v) {
        if($v[&#39;ar_parent_id&#39;] == $id) {
            $subs[] = $v;
            $subs = array_merge($subs,roletree($arr,$v[&#39;ar_id&#39;]));
        }
    }
    return $subs;
}

Then the output is looped on the static page, the effect is as follows:

Whoever adds a subordinate to display the effect will have that subordinate next to him

The above is the detailed content of Introduction to Infinitus classification and sorting methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Horner's LawNext article:Horner's Law