想到了一个树形无限分类数组的实现方式,望指点

#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');获得数组
不知效率如何,目前看来速度还不错,使用的是左右方式的分类方法,希望各位大侠可以指点指点

该贴已经同步到 Pony的微博

2011-03-12 20:46:04

#2 jake

无限树形分类算法,主要就是希望能避免“递归”,因为递归的效率会随着分类层次的深入增多而成几何倍数的减低。左右值,ID路径等算法,都是避免了递归的。

2011-03-13 08:46:59

#3 jiayan41

栏目分类教程.png
栏目分类我通常是这样用的,效率会很低吗?

2011-03-14 08:57:16

#4 hanwj

回复 3 jiayan41


    这个算法直接有效,能否共享一下使用方法

2011-05-06 05:09:06