Example of thinkphp implementing breadcrumb navigation (current location) _ PHP Tutorial

Source: Internet
Author: User
Thinkphp provides an example of how to implement the breadcrumb navigation (current location. In the past, there were very few columns, and they were done in an endless way (the homepage-topic name). now there are too many columns, and the second-level columns and third-level columns are coming. this method is obviously not suitable, as a result, there were very few topics in the past, and they were created in an endless way (the name of the homepage> topic). now there are more columns, and more secondary columns and third-level columns are coming, this method is obviously not suitable, so I improved it. It is not difficult to use a recursive function.

Example:

The code is as follows:


// Current location-the first parameter catid is the id of the current topic, the second parameter is the title of the article, and the second parameter is blank when calling the current position of the topic.
$ This-> assign ("now_here", $ this-> now_here ($ catid, $ res ['title']);

Implementation code:

The code is as follows:

// To explain, the catid in the topic table category is the topic id, catname is the topic name, asmenu is the parent id of the topic, and asmenu is 0 when it is a top-level topic.
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;
}



Appendix: Another example

The code is as follows:


Class IndexAction extends Action {

Public function cat (){
Load ('extend'); // load the extend. php file
// Retrieve all categories
$ Categories = M ('category')-> select ();

$ Nav_array = array ();
$ This-> getNavCrumbs ($ Categories, 2120, $ nav_array );
Dump ($ nav_array );

// Retrieve all categories (and construct a tree)
// $ CategoryTree = list_to_tree ($ Categories, 'Categories _ id', 'parent _ id ');
}

/**
* Create a bread based on the classification id.
* @ Param $ Categories an array composed of all Categories
* @ Param $ categoryId: id of the category to be used for upward backtracking
* @ Param $ navCrumbs: The array used to save the result. 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) {// Here 0 is the root node id (root node id)
$ This-> getNavCrumbs ($ Categories, $ parent_id, $ navCrumbs );
}

$ NavCrumbs [$ categories_id] = $ category;
}

}


Summary (homepage-topic name), now there are more columns, gradually the second-level columns, the third-level columns also come, this method is obviously not suitable, so...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.