#1 Pony
数据表如下web_files_categorys
字段 类型 Null 默认 注释
cateid int(6) 否 文件-分类编号
upid int(6) 否 上级分类编号
name varchar(120) 否 分类名称
cateorder int(10) 是 0 分类排序,数字越大,排序越前
public $tree = array();
/**
* 获取一行分类信息
* @param int $cateid 需要指定分类ID
*/
function getCategory($cateid){
$this -> tbl_name = 'web_files_categorys';
$this -> pk = 'cateid';
$condition = array('cateid'=>$cateid);
return $this -> find($condition);
}
/**
* 获取多行分类信息,需要指定上级分类
* @param int $upid 上级分类ID
*/
function getCategorys($upid){
$this -> tbl_name = 'web_files_categorys';
$this -> pk = 'cateid';
$condition = array('upid'=>$upid);
return $this -> findAll($condition);
}
/**
* 树递归数组
* @param int $currentid 树的顶端分类ID
*/
function combineAsTree($currentid,$currentkey = '["children"]'){
$children = $this -> getCategorys($currentid);
eval('$this -> tree'.$currentkey.' = $children;');
for($i=0;isset($children[$i]);$i++){
$this -> combineAsTree($children[$i]['cateid'],$currentkey.'['.$i.']["children"]');
}
}
/**
* 获取树状数组
* @param int $topid 树的顶端分类ID
*/
function getTree($topid){
$this -> tree = $this -> getCategory($topid);
$this -> combineAsTree($topid);
return $this -> tree;
}
通过使用 getTree('树头分类ID');获得数组
不知效率如何,目前看来速度还不错,使用的是左右方式的分类方法,希望各位大侠可以指点指点
2011-03-12 20:46:04