实现子菜单分类功能

原始数据:

INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('1', '0', '首页', '0', 'Home', 'Index', 'index', '', '', '', '0', '1', '1');
INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('2', '0', '村屋', '0', 'Home', 'Lists', 'index', 'id/1', '', '', '0', '1', '1');
INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('4', '0', '资讯', '2', 'Home', 'News', 'index', '', '', '', '0', '1', '1');
INSERT INTO `cunwu`.`t_navi` (`id`, `type`, `name`, `pid`, `app_name`, `module_name`, `action_name`, `data`, `remark`, `url`, `often`, `ordid`, `display`) VALUES ('5', '0', '土地', '0', 'Home', 'Lists', 'index', 'id/2', '', '', '0', '1', '1');

实现以下结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [type] => 0
            [name] => 首页
            [pid] => 0
            [app_name] => Home
            [module_name] => Index
            [action_name] => index
            [data] => 
            [remark] => 
            [url] => 
            [often] => 0
            [ordid] => 1
            [display] => 1
        )

    [1] => Array
        (
            [id] => 2
            [type] => 0
            [name] => 村屋
            [pid] => 0
            [app_name] => Home
            [module_name] => Lists
            [action_name] => index
            [data] => id/1
            [remark] => 
            [url] => 
            [often] => 0
            [ordid] => 1
            [display] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [type] => 0
                            [name] => 资讯
                            [pid] => 2
                            [app_name] => Home
                            [module_name] => News
                            [action_name] => index
                            [data] => 
                            [remark] => 
                            [url] => 
                            [often] => 0
                            [ordid] => 1
                            [display] => 1
                        )

                )

        )

    [2] => Array
        (
            [id] => 5
            [type] => 0
            [name] => 土地
            [pid] => 0
            [app_name] => Home
            [module_name] => Lists
            [action_name] => index
            [data] => id/2
            [remark] => 
            [url] => 
            [often] => 0
            [ordid] => 1
            [display] => 1
        )

)

思路方法:
1、查询出当前的菜单数据
2、设计递归算法实现计算
3、得出具有二级数组的父子数据
4、嵌套到模板就可以实现

以下是递归算法:

#递归函数
    public  function recur($data,$pid=0){
           $list = [];
          if(is_array($data)){
            $total = count($data);
            foreach($data  as $k=>$v){
                if($v['pid']==$pid){
                  $v['children']=$this->recur($data,$v['id']);
                  if($v['children']==null){
                   unset($v['children']);
                  }
                  $list[] = $v;
                }
            }
          }
          return $list;
    }

以下是应用的示例代码:

$navi_data =M('navi')->where(array('status'=>1))->order('ordid desc')->select();
$navi_list=$this->recur($navi_data);
print_r($navi_list);

下面看实现的效果吧: