微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php无限极分类源码加数据库

PHP无限极分类源码demo本代码可供参考

数据库

CREATE TABLE `my_navigation` (
   `navigation_id` int(11) unsigned NOT NULL AUTO_INCREMENT,   `parent_id` int(11) NOT NULL COMMENT '父id',   `title` varchar(50) NOT NULL COMMENT '名称',   `link` varchar(50) DEFAULT NULL COMMENT '地址',   `icon` varchar(20) DEFAULT NULL COMMENT '图标',   `sort` int(2) DEFAULT NULL COMMENT '排序',   `level` tinyint(2) NOT NULL DEFAULT '1' COMMENT '级别',   `created_at` timestamp NULL DEFAULT NULL,   `updated_at` timestamp NULL DEFAULT NULL,   `deleted_at` timestamp NULL DEFAULT NULL,   PRIMARY KEY (`navigation_id`)
 ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

方法采用递归

/**
 * @param $data
 * @return array
 * 获取分类
 */
static function getTree( $data, $pid )
{
    $arr = array();
    foreach ( $data as $k=>$row )
    {
        if( $row->parent_id == $pid )
        {
            if( $pid != 0 )
            {
                $row->title = static::getB($row->level)."|------- ".$row->title;
            }
            $arr[] = $row;
            $arr = array_merge($arr,self::getTree($data, $row->navigation_id ));
        }
    }
    return $arr;
}

static function getB( $n )
{
    $srt = '';
    for ( $i=0; $i<$n; $i++ )
    { 
      $srt.=$srt.'&nbsp;&nbsp;';
    }
    return $srt;
}

调用

$data 为数组 0为起始父id
static::getTree($data,0);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐