无限分级 方法总结

数据库使用的mysql具体表如下

CREATE TABLE `eb_store_category` (  
    `id` mediumint(11) NOT NULL AUTO_INCREMENT COMMENT '商品分类表ID', 
    `pid` mediumint(11) NOT NULL COMMENT '父id',  
    `name` varchar(100) NOT NULL COMMENT '分类名称',
    PRIMARY KEY (`id`),  
    KEY `pid` (`pid`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='商品分类表'

具体代码如下

//共用的类库
//主要封装了上下级关系处理
class ComUtil{
    /**
     * desc 获取子集link map
     * @param $data array
     * @param $id int
     * @return array 
     */
    public static function getChildMap($data, $id = 0)
    {
        $tree = [];
        foreach($data as $nKey => $nItem){
            if($nItem['pid'] == $id){
                unset($data[$nKey]);
                $tree[] = [
                    'id' => $nItem['id'],
                    'name' => $nItem['name'],
                    'pid' => $nItem['pid'],
                    'children' => self::getChildMap($data, $nItem['id']),
                ];
            }
        }
        return $tree;
    }

    /**
     * desc  获取父集link map 
     * desc pid 是当前分类的id
     * @param $data array
     * @param $pid int
     * @return array
     */
    public static function getParentMap($data, $id)
    {
        $tree = [];
        foreach($data as $nKey => $nItem){
            if($id == $nItem['id']){
                unset($data[$nKey]);
                $tree[] = [
                    'id' => $nItem['id'],
                    'name' => $nItem['name'],
                    'pid' => $nItem['pid'],
                    'parent' => self::getParentMap($data, $nItem['pid']),
                ];
            }
        }
        return $tree;
    }

    /**
     * desc 获取当前分类下级的 ids
     * @param $data array
     * @param $id int
     * @param $isSelf int 0 不包括自己, 1 包括 
     * @return array
     */
    public static function getChildIds($data, $id, $isSelf = 0)
    {
        $tree = [];
        if($isSelf){ $tree[] = $id; }
        foreach($data as $nKey => $nItem){
            if($id == $nItem['pid']){
                unset($data[$nKey]);
                $tree[] = $nItem['id'];
                $tree = array_merge($tree, self::getChildIds($data, $nItem['id'], 0));
                $tree = array_values(array_unique($tree));
            }
        }
        return $tree;
    }

    /**
     * desc 获取当前用户上级的 ids
     * @param $data array
     * @param $id int
     * @param $isSelf int 0 不包括自己, 1 包括 
     * @return array
     */
    public static function getParentIds($data, $id, $isSelf = 0)
    {
        $tree = [];
        foreach($data as $nKey => $nItem){
            if($id == $nItem['id']){
                unset($data[$nKey]);
                //去掉当前查找上级的用户uid
                if($isSelf){ $tree[] = $nItem['id']; }
                $tree = array_merge($tree, self::getParentIds($data, $nItem['pid'], 1));
                $tree = array_values(array_unique($tree));
                if($uid == 0){ break; }
            }
        }
        return $tree;
    }
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

还有一种是有限的无限分类,可以去了解一下。因为这种无限分类,在想查找指定类别下的所有数据的时候,成本比较高

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!