面包屑导航,递归无限级分类

先说下表结构:

CREATE TABLE `menus` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL COMMENT '菜单名称',
  `pid` int(11) NOT NULL COMMENT '父id  顶级为0',
  `level` int(3) NOT NULL COMMENT '菜单等级',
  `flag` int(4) NOT NULL COMMENT '是否可以上传文档 2:不可以(有子菜单) 1:可以(最低级菜单) ',
  `created_at` timestamp NULL DEFAULT '2000-01-01 00:00:00',
  `updated_at` timestamp NULL DEFAULT '2000-01-01 00:00:00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;

下面是使用的类

class PHPTree{
    protected static $config = array(
        /* 主键 */
        'primary_key'     => 'id',
        /* 父键 */
        'parent_key'      => 'parent_id',
        /* 展开属性 */
        'expanded_key'  => 'expanded',
        /* 叶子节点属性 */
        'leaf_key'      => 'leaf',
        /* 孩子节点属性 */
        'children_key'  => 'children',
        /* 是否展开子节点 */
        'expanded'        => false
    );

    /* 结果集 */
    protected static $result = array();

    /* 层次暂存 */
    protected static $level = array();
    /**
     * @name 生成树形结构
     * @param array 二维数组
     * @return mixed 多维数组
     */
    public static function makeTree($data,$options=array() ){
        $dataset = self::buildData($data,$options);
        $r = self::makeTreeCore(0,$dataset,'normal');
        return $r;
    }

    /* 生成线性结构, 便于HTML输出, 参数同上 */
    public static function makeTreeForHtml($data,$options=array()){

        $dataset = self::buildData($data,$options);
        $r = self::makeTreeCore(0,$dataset,'linear');
        return $r;
    }

    /* 格式化数据, 私有方法 */
    private static function buildData($data,$options){
        $config = array_merge(self::$config,$options);
        self::$config = $config;
        extract($config);

        $r = array();
        foreach($data as $item){
            $id = $item[$primary_key];
            $parent_id = $item[$parent_key];
            $r[$parent_id][$id] = $item;
        }

        return $r;
    }

    /* 生成树核心, 私有方法  */
    private static function makeTreeCore($index,$data,$type='linear')
    {
        extract(self::$config);
        foreach($data[$index] as $id=>$item)
        {
            if($type=='normal'){
                if(isset($data[$id]))
                {
                    $item[$expanded_key]= self::$config['expanded'];
                    $item[$children_key]= self::makeTreeCore($id,$data,$type);
                }
                else
                {
                    $item[$leaf_key]= true;
                }
                $r[] = $item;
            }else if($type=='linear'){
                $parent_id = $item[$parent_key];
                self::$level[$id] = $index==0?0:self::$level[$parent_id]+1;
                $item['level'] = self::$level[$id];
                self::$result[] = $item;
                if(isset($data[$id])){
                    self::makeTreeCore($id,$data,$type);
                }

                $r = self::$result;
            }
        }
        return $r;
    }
}

调用(html模式):

$data = array(
    array(
        'id' => 1,
        'name' => '用户管理',
        'parent_id' => 0
    ),
    array(
        'id' => 2,
        'name' => '用户列表',
        'parent_id' => 1
    ),
    array(
        'id' => 3,
        'name' => '权限管理',
        'parent_id' => 1
    ),
    array(
        'id' => 4,
        'name' => '文章管理',
        'parent_id' => 0
    ),
    array(
        'id' => 5,
        'name' => '新闻',
        'parent_id' => 4
    ),
    array(
        'id' => 6,
        'name' => '国内新闻',
        'parent_id' => 5
    )
);

$r = PHPTree::makeTreeForHtml($data);

echo '

PHPTree树形结构

'; echo '';

面包屑导航(laravel中实现):

//存面包屑数组
$menu_nav = Menu::find($request->id);
$nav[] = $menu_nav->title;
$i=0;
while(($menu_nav->pid)>0){
    $menu_nav = Menu::find($menu_nav->pid);
    array_unshift($nav,$menu_nav->title);
}

你可能感兴趣的:(面包屑导航,递归无限级分类)