首页 > 技术文章 > 三字段的无限极分类 + 下拉列表生成器(支持层次显示)

beanliu 2014-10-23 16:29 原文


/**
* 无限极分类
* @return [type] [description]
*/
protected function getSubs($categorys, $catId=0, $level=0)
{
$subs=array();
foreach ($categorys as $item)
{
if ($item['parentId']==$catId)
{
$item['level']=$level;
$subs[]=$item;
$subs=array_merge($subs, $this->getSubs($categorys, $item['id'],$level+1));
}
}
return $subs;
}

/**
* 下拉列表生成器,支持无限极分类
* @param [type] $cate [description]
* @param [type] $selectedId [description]
* @return [type] [description]
*/
protected function cateOptions($cate,$selectedId){
$str='';
if ($cate){
foreach ($cate as $c){
$selected='';
if ($c['id']==$selectedId){
$selected=' selected';
}
$str.='<option value="'.$c['id'].'"'.$selected.'>'.str_repeat('&nbsp;&nbsp;&nbsp;', $c['level']).$c['name'].'</option>';
}
}
return $str;
}

推荐阅读