二級分類的資料表結構如下:
複製代碼 代碼如下:PHP代碼如下
/**
@ 文章分類 含二級分類
@ param int $rootnum -- 一級分類數量
@ param int $childnum -- 二級分類數量
@ 傳回值 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){
//子類數組名為 childcategory 根據情況自訂名稱
$category[] = array('cateid'=>$parent_value['cateid'],'catename'=>$parent_value['catename'],'childcategory'=>array());
//讀取子類
$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頁面調用分類,如index.php
$goodscatetree = array();
$goodscatetree = temp_goodstreecate(4,0); //調用分類函數(含二級分類)4--表示一級分類只顯示4個,0--表示二級分類不限數量
$tpl>assign("goodscatetree",$goodscatetree); //執行smarty引擎
$tpl->display->(index.tpl); //輸出smarty模版頁面
TPL模版頁面輸出分類,如index.tpl頁面
{section name=p loop=$goodscatetree}
一級分類:{$goodscatetree[p].catename}
{section name=c loop=$goodscatetree[p].childcategory}
二級分類:{$goodscatetree[p].childcategory[c].catename}
{/section}
{/section}