时间:2021-07-01 10:21:17 帮助过:9人阅读
实现代码:
代码如下:
//解释一下,栏目表category中的catid为栏目id,catname为栏目名称,asmenu为栏目父级的id,当为顶级栏目时,asmenu为0 。
protected function now_here($catid,$ext=''){
$cat = M("Category");
$here = '首页';
$uplevels = $cat->field("catid,catname,asmenu")->where("catid=$catid")->find();
if($uplevels['asmenu'] != 0)
$here .= $this->get_up_levels($uplevels['asmenu']);
$here .= ' -> '.$uplevels['catname']."";
if($ext != '') $here .= ' -> '.$ext;
return $here;
}
protected function get_up_levels($id){
$cat = M("Category");
$here = '';
$uplevels = $cat->field("catid,catname,asmenu")->where("catid=$id")->find();
$here .= ' -> '.$uplevels['catname']."";
if($uplevels['asmenu'] != 0){
$here = $this->get_up_levels($uplevels['asmenu']).$here;
}
return $here;
}
附:另一个例子
代码如下:
class IndexAction extends Action {
public function cat() {
load('extend'); // 加载 extend.php 文件
// 取出所有的分类
$Categories = M('Categories')->select();
$nav_array = array();
$this->getNavCrumbs($Categories, 2120, $nav_array);
dump($nav_array);
// 取出所有分类(并构造成一棵树)
// $CategoryTree = list_to_tree($Categories, 'categories_id', 'parent_id');
}
/**
* 根据分类id向上回溯构造面包屑
* @param $Categories 由所有分类组成的数组
* @param $categoryId 要进行向上回溯用的分类id
* @param $navCrumbs 用于保存结果的数组,传入一个空数组就好
*/
public function getNavCrumbs($Categories, $categoryId, &$navCrumbs) {
$category = list_search( $Categories, array('categories_id'=>$categoryId) ) ;
$category = $category[0];
$parent_id = $category['parent_id'];
$categories_id = $category['categories_id'];
if( $parent_id != 0 ) { // 这里的 0 是根节点id(root节点id)
$this->getNavCrumbs($Categories, $parent_id, $navCrumbs);
}
$navCrumbs[$categories_id] = $category;
}
}