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.' ';
}
return $srt;
}调用
$data 为数组 0为起始父id static::getTree($data,0);
本文由:xiaoshu168 作者:xiaoshu发表,转载请注明来源!