• 三级数组查询


    $data = db::name('AuthRule')->select();
    $data = list_to_tree2($data,'id','pid');//aaa($data);
    // halt($data);
    $this->assign("nodeList", $data);
    return $this->fetch();

    /**
     * 把返回的数据集转换成Tree
     * @access public
     * @param array $list 要转换的数据集
     * @param string $pid parent标记字段
     * @param string $level level标记字段
     * @return array
     */
    function list_to_tree2($list, $pk='id',$pid = 'pid',$child = '_child',$root=0) {
        // 创建Tree
        $tree = array();
        if(is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];//取出每个栏目id作为键名,
            }
            foreach ($list as $key => $data) {//开始循环,key从0开始一个一个往下走0123.。。
                // 判断是否存在parent
                $parentId = $data[$pid];//echo $parentId.'|';//0|112|116|117|112输出每个数组里的pid
                if ($root == $parentId) {//key从0开始循环,如果是一级栏目pid==0
                    $tree[] =& $list[$key]; //用连接符,吧数组放到tree里面
                }else{
                //如果pid不是0,那就是二级栏目
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];//用pid去refer数组,匹配出父栏目数组,用连接符拼接到这里
                        $parent[$child][] =& $list[$key];aaa($parent);//将当前二级栏目放到父栏目下,拼接成二维数组
                    }
                //仔细看list数组数据格式,第一遍key是0开始循环走的是tree哪里,
                //第二遍从1开始走,走到parent这里,
                }
            }
        }
        return $tree;
    }

    打印出的数据格式:

    $list = Array
    (
        [0] => Array
            (
                [id] => 112
                [name] => /Admin
                [title] => 后台
                [type] => 1
                [node_icon] => 
                [class_icon] => 
                [status] => 1
                [condition] => 
                [sort] => 0
                [pid] => 0
                [level] => 1
                [update_time] => 1532081114
                [create_time] => 1510821470
                [is_display] => 1
            )
    
        [1] => Array
            (
                [id] => 117
                [name] => /Admin/Index/index
                [title] => 后台首页
                [type] => 1
                [node_icon] => 
                [class_icon] => 
                [status] => 1
                [condition] => 
                [sort] => 0
                [pid] => 116
                [level] => 3
                [update_time] => 1510821714
                [create_time] => 1510821714
                [is_display] => 1
            )
    }
    
    $refer = Array
    (
        [112] => Array
            (
                [id] => 112
                [name] => /Admin
                [title] => 后台
                [type] => 1
                [node_icon] => 
                [class_icon] => 
                [status] => 1
                [condition] => 
                [sort] => 0
                [pid] => 0
                [level] => 1
                [update_time] => 1532081114
                [create_time] => 1510821470
                [is_display] => 1
            )
    
        [117] => Array
            (
                [id] => 117
                [name] => /Admin/Index/index
                [title] => 后台首页
                [type] => 1
                [node_icon] => 
                [class_icon] => 
                [status] => 1
                [condition] => 
                [sort] => 0
                [pid] => 116
                [level] => 3
                [update_time] => 1510821714
                [create_time] => 1510821714
                [is_display] => 1
            )
    )
    
    $tree = Array
    (
        [0] => Array
            (
                [id] => 112
                [name] => /Admin
                [title] => 后台
                [type] => 1
                [node_icon] => 
                [class_icon] => 
                [status] => 1
                [condition] => 
                [sort] => 0
                [pid] => 0
                [level] => 1
                [update_time] => 1532081114
                [create_time] => 1510821470
                [is_display] => 1
            )
    
    )
    
    
    $parent = Array
    (
        [id] => 116
        [name] => /Admin/Index
        [title] => 首页
        [type] => 1
        [node_icon] => 
        [class_icon] => 
        [status] => 1
        [condition] => 
        [sort] => 1
        [pid] => 112
        [level] => 2
        [update_time] => 1510821685
        [create_time] => 1510821685
        [is_display] => 1
        [is_status] => 1
        [_child] => Array
            (
                [0] => Array
                    (
                        [id] => 117
                        [name] => /Admin/Index/index
                        [title] => 后台首页
                        [type] => 1
                        [node_icon] => 
                        [class_icon] => 
                        [status] => 1
                        [condition] => 
                        [sort] => 0
                        [pid] => 116
                        [level] => 3
                        [update_time] => 1510821714
                        [create_time] => 1510821714
                        [is_display] => 1
                        [is_status] => 1
                    )
    
            )
    
    )
    

      

     页面:

    <dt>管理权限</dt>
    <dd>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tab rules-data" data-rules="{present name ="$info['rules']"}{$info['rules']}{/present}">
    {foreach name="nodeList" item="level1"}
    <tr>
        <td style="font-size: 14px;"><label><input name="rules[]" level="1" type="checkbox" obj="node_{$level1.id}" value="{$level1.id}" data="{$level1.id}:1:0" data-id="{$level1.id}" data-pid="0"/> <b class="mac-size">[项目]</b> {$level1.title}</label></td>
    </tr>
    {present name="level1['_child']"}
    {foreach name="level1['_child']" item="level2" }                         
        <tr>
            <td style="padding-left:60px; font-size: 14px;"><label><input name="rules[]" level="2" type="checkbox" obj="node_{$level1.id}_{$level2.id}" value="{$level2.id}"  data="{$level2.id}:2:{$level2.pid}" data-id="{$level2.id}" data-pid="{$level2.pid}"/> <b class="mac-size">[模块]</b> {$level2.title}</label></td>
        </tr>
    	{present  name="$level2['_child']"}
    	    {volist name="level2['_child']" id="level3"} 
    	    <tr ><td style="padding-left: 120px;">
    	    	<label><input name="rules[]" level="3" type="checkbox" obj="node_{$level1.id}_{$level2.id}_{$level3.id}" value="{$level3.id}" data="{$level3.id}:3:{$level3.pid}" data-id="{$level3.id}" data-pid="{$level3.pid}"/>  <b class="mac-size">[操作]</b>{$level3.title}</label>
    	    </td></tr>
                 {present  name="$level3['_child']"} 
               	<tr>
        		  <td style="padding-left: 160px;">
               	    {foreach name="level3['_child']" item="level4" key="k" }
               	    {if condition="$level4.is_display eq 1"}|   {/if}
    					<label>
    						<input name="rules[]" level="4" type="checkbox" obj="node_{$level1.id}_{$level2.id}_{$level3.id}_{$level4.id}" value="{$level4.id}" data="{$level4.id}:4:{$level4.pid}" data-id="{$level4.id}" data-pid="{$level4.pid}"/> {$level4.title}    
    					</label> 
               	    {/foreach}
               	  </td>
    			</tr> 
               {/present}
            {/volist} 
       {/present}
    {/foreach}
    {/present}
    {/foreach}
    

      

  • 相关阅读:
    关于git---远程
    关于git---主要
    css特效
    Canvas图片压缩
    TypeScript简单介绍
    html 常见兼容性问题及解决方法
    cookies,sessionStorage 和 localStorage 的区别
    vue-element-admin vue.config.js
    ② nodejs + mongodb 搭建服务器
    ① 数据自动填充
  • 原文地址:https://www.cnblogs.com/yszr/p/9402564.html
Copyright © 2020-2023  润新知