zoukankan      html  css  js  c++  java
  • [置顶] 递归 加引用 实现tree 和 无限级菜单

    <?php
    class k_model_menu_menu
    {
        private $data = array();
        private $rdata = array();
        private $jdata = array();
        private $level = 0;
        private $paret = array();
        function getOption($type= 'part',$pid = 0)
        {
            if($type = 'all') $this->data= R::getAll( 'select * from menu' );
            
            $this->teamData();
            
            if($this->rdata) return $this->rdata;
            return false;    
        }
        
        function addMenu($data){
            foreach($data as $key =>$value){
                if($value == '请填写内容!') $data[$key]='';
            }
            if($data){
                $menu = R::dispense('menu');
                $menu->pid = $data['pid'];
                $menu->name = $data['name'];
                $menu->url = $data['url'];
                $menu->icon = $data['icon'];
                $id = R::store($menu);
                return $id;
            }
        }
        //返回json 字符串
        public function getJsonMenu(){
            $data = $this->getChild(1);
            $this->jdata = $data;
            $this->recursive($this->jdata);
            return  json_encode($this->jdata);
        }
            
        //递归函数  实现不断的生成子节点,用了引用,感觉这引用是如来神笔,要不然实现太复杂了
        public function recursive(&$data = array()){
            foreach($data as $key =>$value){
                $data[$key]['children']= $this->getChild($value['id']);
                $tmp = &$data[$key]['children'];
                if($tmp){
                    $this->recursive($tmp);
                }
            }
        }
        
        //组织数据,用于生成树形的select 返回的是一个数组
        //数组的形式是
            public function teamData($pid=1){
                foreach ($this->data as $key => $value) {    
                    if($value['pid']==$pid){
                            $this->level++;
                            array_push($this->rdata, array('name'=>$value['name'],'level'=>$this->level,'id'=>$value['id']));
                                    
                        $tmpdata = $this->teamData($value['id']);
                        if(!$tmpdata){
                            $this->level--;
                            continue;
                        }
                    }
                }    
            }
            
        
        //根据pid拿取下面的子数据
        public function getChild($pid){
             $data=  R::getAll( "select * from menu where pid = {$pid}" );
             $tmpdata = array();
             if($data){
                 foreach ($data as $key => $value) {
                     $tmpdata[$key]['id'] =  $value['id'];
                     $tmpdata[$key]['icon'] =  $value['icon'];
                     $tmpdata[$key]['text'] =  $value['name'];
                     $tmpdata[$key]['url'] =  $value['url'];
                     $tmpdata[$key]['children'] =  array();
                 }
             }
             return $tmpdata;
        }
        
        
    }

  • 相关阅读:
    【原】error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'
    【原】开发路上疑难BUG调试记录
    【转】XCode: duplicate symbol 解决方案
    【原】就IOS发布app时如何保护文本资源的一个方法
    【原】xcode5.0升级5.1遇到的clang: error: unknown argument: '-fobj-arc'错误
    【转】生产者与消费者
    安卓快速排序与冒泡排序
    数据结构之算法时间复杂度
    常见数据结构与算法整理总结(上)
    Activity的onPause()、onStop()和onDestroy()里要做的事情
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3290044.html
Copyright © 2011-2022 走看看