無限分級 方法總結

p_burt發表於2020-03-26

資料庫使用的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 協議》,轉載必須註明作者和本文連結

相關文章