?php
//計(jì)算某個(gè)類別所屬的類別層數(shù)
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);//任務(wù)數(shù)組
$tree=array();//結(jié)果數(shù)組
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;
}
//查詢類別所屬級(jí)
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
{
/**
* 分類列表頁(yè)(遞歸)
*/
public function category_list()
{
header('content-type:text/html;charset=utf-8');
$categoryArr=db::name('表名')->select();
//調(diào)用遞歸函數(shù)
$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">此處是分頁(yè)/div>
/div>