<?php
/*使用递归方式遍历无限分级类*/
//必须获得顶级栏目
//加载数据库类
$ROOT = substr(dirname(__FILE__),0,-4);
include $ROOT.'/include/common.inc.php';
$sql = "SELECT id,typename FROM dede_arctype where reid=0 ORDER BY id ASC";
$dsql->Execute('me',$sql);
$tpc = '';
while($row = $dsql->GetArray('me')){
$topc[$row['id']] = $row;
$topid[] = $row['id'];
blc($row['id'],null);
}
function blc($pid,$ppid)
{
global $dsql , $topc;
$rows = '';
$sql = "SELECT id,reid,typename FROM dede_arctype where reid=$pid ORDER BY id ASC";
$dsql->Execute('idme',$sql);
while($row = $dsql->GetArray('idme')){
$topc[$pid]['son'][$row['id']] = $row;
$topc[$ppid]['son'][$pid]['son'][$row['id']] = &$topc[$pid]['son'][$row['id']];
$rows[] = $row;
}
if($rows == '')
{
return;
}
else
{
foreach($rows as $varid){
blc($varid['id'],$varid['reid']);
}
return;
}
}
$topkey = array_keys($topc);
$toplength = count($topkey);
foreach($topid as $now_id)
{
for($i=0;$i < $toplength;$i++)
{
if($topkey[$i] == $now_id) $reTOP[$topkey[$i]] = $topc[$topkey[$i]];
}
}
unset($topc);
$str = '';
function echotra($row){
global $str;
//if(empty($row['son'])) return;
foreach($row as $id=>$value){
$str .= '<li>'.$value['typename'].'</li>';
if($value['son']){
$str .='<ul>';
echotra($value['son']);
$str .= '</ul>';
}
}
}
echotra($reTOP);
echo $str;
非递归类
<?php
$ROOT = substr(dirname(__FILE__),0,-4);
include $ROOT.'/include/common.inc.php';
$sql = "SELECT id,reid,typename FROM dede_arctype ORDER BY `id` ASC";
$dsql->Execute('me',$sql);
$rows = array();
while ($row = $dsql->GetArray('me')) {
$rows[$row['id']] = $row;
}
function traversal(){
global $rows, $dsql;
$t = array();
foreach ($rows as $id => $item) {
if ($item['reid']) {
$rows[$item['reid']]['son'][] = &$rows[$item['id']];
$t[] = $id;
}
}
foreach($t as $u) {
unset($rows[$u]);
}
$rows;
}
traversal();
$str = '';
//以ul标签形式输出栏目树
function echotra($row){
global $str;
//if(empty($row['son'])) return;
foreach($row as $id=>$value){
$str .= '<li>'.$value['typename'].'</li>';
if($value['son']){
$str .='<ul>';
echotra($value['son']);
$str .= '</ul>';
}
}
}
//echo tra($rows);
echo $str;