无限级分类处理类

9-03 500 views

根据数据库里面的分类递归获取子类

<?php
/*
*静态化方法
*/
class Cate {

    //静态方法获取一维数组递归结果
    static Public function unlimitedForLevel($cate,$html='',$pid=0,$level=0,$parent_name = ''){
        //建立空数组存储结果
        $arr = array();
        //循环$cate,如果这级的pid等于上一级的cid,就先压入数组,再找当前的下一级
        foreach ($cate as $v) {
            if ($v['parent_id'] == $pid) {
                $v['level'] = $level + 1;
//                $v['html'] = str_repeat($html, $level);
                if ($v['level'] > 1) {
                    if ($v['level'] == 2) {
                        $v['html'] = '|-   ';
                    } else {
                        $v['html'] = str_repeat('|', $level - 1 ).'   |-';
                    }

                } else {
                    $v['html'] = '';
                }
                $v['parent_name'] = $parent_name;
                $arr[] = $v;
                //递归合并数组
                $arr = array_merge($arr,self::unlimitedForLevel($cate,$html,$v['id'],$level+1,$v['name']));
            }
        }

        return $arr;
    }

    //静态方法获取多维数组递归结果
    static Public function unlimitedForMore($cate,$name='child',$pid=0){
        $arr = array();
        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                $v[$name] = self::unlimitedForMore($cate,$name,$v['cid']);
                $arr[] = $v;
            }
        }
        return $arr;
    }

    //递归获取父函数
    static Public function getParents($cate,$cid){
        $arr = array();
        foreach ($cate as $v) {
            if ($v['id'] == $cid) {
                $arr[] = $v;
                $arr = array_merge(self::getParents($cate,$v['parent_id']),$arr);
            }
        }
        return $arr;
    }

}

yum 安装redis5

更新yum源 # EPEL的全称叫 Extra Packages for Enterprise Linux 。EPEL是由 Fedora 社区打造,为 RHEL 及衍生发行版如 CentOS、Scientific Linux 等提供高质...

阅读全文

nginx 解析php conf配置文件

本博客的nginx 配置文件 server { listen 80;#80和443端口同时监听 listen 443 ssl; server_name www.koukousky.com koukousky.com...

阅读全文

php composer 安装阿里云镜像

#doc: https://developer.aliyun.com/composer 全局配置(推荐) composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 取消配...

阅读全文

欢迎留言