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;
}
}