快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

目录tree在页面上显示

//完整的目录结构
    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="";
        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_end = "
"; $li_begin = "
  • ";//%s处是dir_name // $li_begin = "
  • %s";//%s处是dir_name $li_end = "
  • "; $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 ; }

    网站标题:目录tree在页面上显示
    地址分享:http://6mz.cn/article/pssooh.html

    其他资讯