?php
//计算某个类别所属的类别层数
function getcatelayer($cateid,$flag=1){
$cates = M('Gcategory');
$ini['cate_id'] = $cateid;
$arr = $cates->where($ini)->find();
if($arr['parent_id']!=0){
$flag = getcatelayer($arr['parent_id'],$flag+1);
}
return $flag;
}
//类别递归(单层)
function getone($arr,$parent=0,$lev=0){
//迭代
$task=array($parent);//任务数组
$tree=array();//结果数组
while (!empty($task)){
$flg=false;
foreach ($arr as $k=>$v){
if ($v['parent_id']==$parent) {
$tree[]=array(
'id'=>$v['cate_id'],
'name'=>$v['cate_name'],
'lev'=>$lev
);
array_push($task, $v['cate_id']);
$parent=$v['cate_id'];
$lev=$lev+1;
unset($arr[$k]);
$flg=true;
}
}
if ($flg==false) {
array_pop($task);
$parent=end($task);
$lev=$lev-1;
}
}
return $tree;
}
//类别递归(多层)
function getCates($arr,$pid=0)
{
for($i=0; $icount($arr); $i++){
if($arr[$i]['parent_id']==$pid){
$newArr[] = array(
"id"=>$arr[$i]['cate_id'],
"name"=>$arr[$i]['cate_name'],
'son'=>getCates($arr,$arr[$i]['cate_id']),
);
}
}
return $newArr;
}
//查询类别所属级
function getlayer($cateid,$flg=1)
{
$gcg=M("Gcategory");
$ini["cate_id"]=$cateid;
$cateArr=$gcg->where($ini)->find();
if($cateArr['parent_id']!=0)
{
$flg=getlayer($cateArr['parent_id'],$flg+1);
}
return $flg;
}
?php
//分类管理
namespace app\admin\Controller;
use think\Db;
use think\Controller;
class CategoryController extends Controller
{
/**
* 分类列表页(递归)
*/
public function category_list()
{
header('content-type:text/html;charset=utf-8');
$categoryArr=db::name('表名')->select();
//调用递归函数
$getall=getone($categoryArr);
$this->assign("getall",$getall);
return $this->fetch()
}
div class="conShow">
table border="1" cellspacing="0" cellpadding="0">
tr>
td width="100px" class="tdColor tdC">id/td>
td width="150px" class="tdColor">汽车类型/td>
td width="180px" class="tdColor">操作/td>
/tr>
{foreach name="getall" id="v"}
tr>
td>{$v.id}/td>
td >{php}echo str_repeat('nbsp;',$v['lev']*2){/php}|-{$v['name']}/td>
td>a href="connoisseuradd.html" rel="external nofollow" >img class="operation"
src="/img/admin/update.png">/a> img class="operation delban"
src="/img/admin/delete.png">/td>
/tr>
{/foreach}
/table>
div class="paging">此处是分页/div>
/div>