二級(jí)分類(lèi)的數(shù)據(jù)表結(jié)構(gòu)如下:
復(fù)制代碼代碼如下:
PHP代碼如下
/**
@ 文章分類(lèi) 含二級(jí)分類(lèi)
@ param int $rootnum -- 一級(jí)分類(lèi)數(shù)量
@ param int $childnum -- 二級(jí)分類(lèi)數(shù)量
@ 返回值 array
@ date 2011.2.24
*/
function temp_articletreecate($rootnum,$childnum){
if(!isnumber($rootnum)){
$rootnum = 10;
}
if(!isnumber($childnum)){
$childnum = 10;
}
$category = array();
$parent_sql = "SELECT cateid,catename FROM ".TABLE_PREFIX."articlecate WHERE parentid=0 AND depth=0 AND flag=1 ORDER BY orders ASC";
if(intval($rootnum)>0){
$parent_sql.=" LIMIT $rootnum";
}
$parent_cate = $GLOBALS['db']->getall($parent_sql);
foreach($parent_cate as $parent_key => $parent_value){
//子類(lèi)數(shù)組名為 childcategory 根據(jù)情況自定義名稱(chēng)
$category[] = array('cateid'=>$parent_value['cateid'],'catename'=>$parent_value['catename'],'childcategory'=>array());
//讀取子類(lèi)
$child_sql = "SELECT cateid,catename FROM ".TABLE_PREFIX."articlecate WHERE parentid=".$parent_value['cateid']." AND flag=1 ORDER BY orders ASC";
if(intval($childnum)>0){
$child_sql.=" LIMIT $childnum";
}
$child_cate = $GLOBALS['db']->getall($child_sql);
foreach($child_cate as $child_key => $child_value){
$category[count($category)-1]['childcategory'][] = array('cateid'=>$child_value['cateid'],'catename'=>$child_value['catename']);
}
}
return $category;
}
PHP頁(yè)面調(diào)用分類(lèi),如index.php
$goodscatetree = array();
$goodscatetree = temp_goodstreecate(4,0); //調(diào)用分類(lèi)函數(shù)(含二級(jí)分類(lèi))4--表示一級(jí)分類(lèi)只顯示4個(gè),0--表示二級(jí)分類(lèi)不限數(shù)量
$tpl>assign("goodscatetree",$goodscatetree); //執(zhí)行smarty引擎
$tpl->display->(index.tpl); //輸出smarty模版頁(yè)面
TPL模版頁(yè)面輸出分類(lèi),如index.tpl頁(yè)面
{section name=p loop=$goodscatetree}
一級(jí)分類(lèi):{$goodscatetree[p].catename}
{section name=c loop=$goodscatetree[p].childcategory}
二級(jí)分類(lèi):{$goodscatetree[p].childcategory[c].catename}
{/section}
{/section}