tp5 无限分类

Model层

namespace app\admin\model;
use think\Model;

class Cate extends Model{
    public function catetree(){
        $cateres = $this->select();
        return $this->sort($cateres);
    }

    public function sort($data,$pid=0,$level=0){
        static $arr = array();
        foreach($data as $k=>$v){
            if($v['pid'] == $pid){
                $v['level'] = $level;
                $arr[] = $v;
                $this->sort($data,$v['id'],$level+1);
            }
        }
        return $arr;
    }
}

Controller层

namespace app\admin\controller;
use app\admin\model\Cate as CateModel;
class cate extends Common{
    public function lst(){
        $cate = new CateModel();
        $cateres = $cate->catetree();
        $this->assign('cateres',$cateres);
        return $this->fetch();
    }
}    

view层

{volist name="cateres" id="vo"}
    <tr>
       <td >  
           {if condition="$vo['level'] neq 0"} |{/if} 
           <?php echo str_repeat('-',$vo['level'] * 8) ?>  
           {$vo.catename} 
       </td>
    </tr>
{/volist}

猜你喜欢

转载自blog.csdn.net/qq_35285627/article/details/80984415
tp5