Home > Article > Backend Development > Thinkphp implements breadcrumb navigation (current location) example sharing_PHP tutorial
In the past, there were only a few columns, so we just used the blind method (Home Page -> Column Name). Now there are more columns, and gradually second-level columns and third-level columns are also coming. This method is obviously not suitable, so Just improved it. It's not difficult, just use a recursive function.
Usage example:
Implementation code:
//Explain that catid in the column table category is the column id, catname is the column name, and asmenu is the id of the column parent. When it is a top-level column, asmenu is 0.
protected function now_here($catid,$ext=''){
$cat = M("Category");
$here = 'Homepage';
$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;
}
Attachment: Another example
public function cat() {
load('extend'); // Load the extend.php file
// Take out all categories
$Categories = M('Categories')-> select();
$nav_array = array();
$this->getNavCrumbs($Categories, 2120, $nav_array);
dump($nav_array);
// Take out all categories (and construct them into a tree)
// $CategoryTree = list_to_tree($Categories, 'categories_id', 'parent_id');
}
/**
* Construct breadcrumbs based on category id backwards
* @param $Categories Array composed of all categories
* @param $categoryId Category id to be used for upward traceback
* @param $navCrumbs The array used to save the results, just pass in an empty array
*/
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 here is the root node id (root node id)
$this->getNavCrumbs($Categories, $parent_id, $navCrumbs);
}
$navCrumbs[$categories_id] = $category;
}
}