温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

目录tree在页面上显示

发布时间:2020-05-06 08:36:39 来源:网络 阅读:41669 作者:jack20039 栏目:web开发
//完整的目录结构
    function get_dir_tree($uid,$from_dir=0){
        $sql = "select dir_id,dir_name,depth,p_id from $this->_dir_table where user_id=$uid and is_del=0 and dir_id>=$from_dir";
        $res = $this->db->query($sql)->result_array();
        if(!isset($res[0])){
            return null;
        }
        $this->load->helper('array');
        //获取depth区间
        $max_depth = 0;
        foreach($res as $dps => $d){
            if($d['depth']>$max_depth){
                $max_depth = $d['depth'];
            }
        }
        for($i=$max_depth;$i>=0;$i--){ //处理成树形,子集存在child里,有没有自己看有没有child字段
            foreach($res as $key=>$val){
                if($val['depth']==$i){
                    foreach($res as $k=>$v){
                        if($val['p_id']==$v['dir_id']){
                            $v['child'][] = $val;
                            $res[$k] = $v;
                            unset($res[$key]);
                        }
                    }
                }
            }
        }
        $return =  $this->has_child_then_add_ul($res);
        $html="<ul>
            <!-- 第一级 -->
            <li class=''><div class='tree-title'><a href='javascript:void(0)' class='icon icon-add'></a><a href='javascript:void(0)' class='shareItem fold'>全部文件</a></div>";
        $html.=$return."</li></ul>";
        return $html;
    }
    private function has_child_then_add_ul($res,$html='',$collaps_times=1){
        $collaps = 15;//前端写的是m_l_15, m_l_30 这样的 , 15为一个单位,跟前端商定就好
        $indent = $collaps_times*$collaps;
        $has_sibling = 0;
        $ul_begin = "<ul class='undis'>";
        $ul_end = "</ul>";
        $li_begin = "<li><div class='tree-title'><a href='javascript:void(0)' class='icon icon-add m_l_%d'></a><a href='javascript:void(0)' class='shareItem fold'>%s</a></div>";//%s处是dir_name
        // $li_begin = "<li>%s";//%s处是dir_name
        $li_end = "</li>";
        $count_li_end = $count_ul_end = 0;
        foreach($res as $key=>$val){
            if(0==$key){
                $html.=$ul_begin;
            }
            $html.=sprintf($li_begin,$indent,$val['dir_name']);
            $count_li_end++;
            if(isset($val['child'])){
                $html = $this->has_child_then_add_ul($val['child'],$html,$collaps_times+1);
            }
            $html.=$li_end;
            if(!isset($res[$key+1])){
                $html.=$ul_end;
            }
        }
        return $html ;
    }


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI