Home  >  Article  >  php教程  >  php无限分类

php无限分类

WBOY
WBOYOriginal
2016-06-06 19:50:521624browse

很早之前用过的一个php无限分类方法,采用的是递归的思路,这里分享给大家 //$data为从数据库取出的分类数据,$pid为上级分类id,$level为分类的层级,这里限定为10级,$keyword为分类名 function genCate($data, $pid =0, $level = 0,$keyword){if($level ==

很早之前用过的一个php无限分类方法,采用的是递归的思路,这里分享给大家

//$data为从数据库取出的分类数据,$pid为上级分类id,$level为分类的层级,这里限定为10级,$keyword为分类名

function genCate($data, $pid =0, $level = 0,$keyword)
{
if($level ==10)break;
$l        = str_repeat("    ", $level);
$l        = $l.'└';
static $arrcat  = array();
$arrcat   =empty($level) ? array() :$arrcat;
foreach($data as $k => $row)
	{
	if($row['pid']==$pid)
		{
		$row[$keyword]  = $l.$row[$keyword];
		$row['level']   =$level;
		$arrcat[]     = $row;
		genCate($data,$row['id'], $level+1,$keyword);
		}
	}
	return $arrcat;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn