Home > Backend Development > PHP Tutorial > Thinkphp implements breadcrumb navigation (current location) example sharing_PHP tutorial

Thinkphp implements breadcrumb navigation (current location) example sharing_PHP tutorial

WBOY
Release: 2016-07-13 10:29:58
Original
1053 people have browsed it

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:

Copy code The code is as follows:

//Current position - The first parameter catid is the id of the current column, and the second parameter is the title of the article. When calling the current position of the column, the second parameter can be empty.
$this->assign("now_here",$this->now_here($catid,$res['title']));

Implementation code:

Copy code The code is as follows:

//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

Copy code The code is as follows:

class IndexAction extends Action {

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

}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/768136.htmlTechArticleIn the past, there were only a few columns, so we just used blind methods (home page - column name), but now there are more columns. Gradually, second-level columns and third-level columns also came. This method was obviously not suitable, so...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template