Skip to content
关注公众号,获取新课通知

商品分类列表


控制器 app/controller/admin/Category.php

php
protected $excludeValidateCheck = ['index'];
public function index()
{
    $param = $this->request->param();
    $list = $this->M->order([
        'order'=>'desc',
        'id'=>'desc'
    ])->select();
	$list = list_to_tree2($list->toArray(),'category_id');
    return showSuccess($list);
}

路由 router/admin.php

php
Route::get('category','admin.Category/index');

公共函数 app/common.php

php
/**
* 数据集组合分类树(多维数组)
* @param     cate 分类结果集
* @param     child 子树
* @return    array
*/
function list_to_tree2($cate,$field = 'pid',$child = 'child',$pid = 0,$callback = false){
	if(!is_array($cate)) return [];
    $arr = [];
    foreach($cate as $v){
    	$extra = true;
    	if(is_callable($callback)){
        	$extra = $callback($v);
        }
        if($v[$field] == $pid && $extra){
            $v[$child] = list_to_tree2($cate,$field,$child,$v['id'],$callback);
            $arr[]     = $v;
        }
    }
    return $arr;
}