zoukankan      html  css  js  c++  java
  • 无限极分类算法 thinkphp

    <?php
    /*
    * 本类实现的是无限级递归分类的管理
    */
    class InfiniteLevel{

    public $id_str="";
    public function add_top($modelName,$data,$id=0){//添加顶级分类
    $model = D($modelName);
    $data['parent_id_str'] = ',0,';
    $data['depth'] = '0';
    $data['create_time'] = time();
    if ($id>0) {
    $model->where('id="'.$id.'"')->save($data);
    }else{
    $id = $model->add($data);
    }
    return $id;
    }
    public function add_child($modelName,$data,$parentId=0){
    $model = D($modelName);
    if ($parentId>0) {
    $tempModel = $model->where(array('id'=>$parentId))->find();
    $depth = $tempModel['depth']+1;
    $parent_id_str = $tempModel['parent_id_str'].$parentId.',';
    $data['parent_id_str'] = $parent_id_str;
    $data['depth'] = $depth;
    $data['create_time'] = time();
    $id = $model->add($data);
    return $id;
    }
    }
    public function move(){}  //项目暂时用不上,以后再写把
    public function delete(){} //项目暂时用不上,以后再写把

    //1
    public function list_model($modelName){
    $model = D($modelName);
    $list = $this->_all_child($modelName);
    return urldecode(urldecode(json_encode($list)));
    }
    //2
    private function _all_child($modelName,$parentId=0,$depth=0,$data=NULL){
    $model = D($modelName);
    $data['parent_id'] = $parentId;
    $data['depth'] = $depth;
    $data['is_deleted'] =0;
    $childArr = $model->field('id,name,parent_id,parent_id_str,depth')->where($data)->select();

    $maxNum = count($childArr);
    if ($maxNum>0) {
    foreach ($childArr as $key => $value) {
    $value['name'] = urlencode($value['name']);
    $item = $this->_all_child($modelName,$value['id'],($value['depth']+1),$childArr);
    if ($item) {
    foreach ($item as $k => $v) {
    $v['name'] = urlencode($v['name']);
    $item[$k] = $v;
    }
    $value['child'] = $item;
    $childArr[$key] = $value;
    }
    }

    return $childArr;
    }else{
    return;
    }
    }

    //
    public function _self_child($modelName,$parentId=0,$num=0){

    $model = D($modelName);
    $data['parent_id'] = $parentId;
    $data['is_deleted'] =0;
    $childArr = $model->field('id,name,parent_id,parent_id_str,depth')->where($data)->select();
    if($num==0){
    $id_str=$parentId.",";
    }else{
    $id_str="";
    }

    $maxNum = count($childArr);
    if ($maxNum>0) {

    foreach ($childArr as $key => $value) {
    $id_str.=$value['id'].",";
    $item = $this->_self_child($modelName,$value['id'],$num=1);
    $id_str.=$item;

    }
    return $id_str;
    }else{
    return;
    }
    }
    }

  • 相关阅读:
    html5+css3中的background: -moz-linear-gradient 用法 (转载)
    CentOS 安装Apache服务
    Linux 笔记
    CURL 笔记
    Spring Application Context文件没有提示功能解决方法
    LeetCode 389. Find the Difference
    LeetCode 104. Maximum Depth of Binary Tree
    LeetCode 520. Detect Capital
    LeetCode 448. Find All Numbers Disappeared in an Array
    LeetCode 136. Single Number
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4860370.html
Copyright © 2011-2022 走看看