Introduction to Infinitus classification and sorting methods

巴扎黑
Release: 2023-03-14 10:30:02
Original
1275 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template