thinkphp Implement Crumb Navigation (Current Location) Example Sharing

  • 2021-06-28 08:47:54
  • OfStack

Previously there were few columns and they were done by dead ends (Home- > Column name). Now there are more columns, Grade 2 columns and Grade 3 columns are coming. This is obviously not a suitable way, so it has been improved.It's not difficult, just use a recursive function.

Example use:


// current location - No. 1 Parameters  catid For Current Column id , 2 Argument is the title of the article, when calling the current position of the column 2 Empty parameters. 
$this->assign("now_here",$this->now_here($catid,$res['title']));

Implementation code:


// explain 1 Down, Column Table category In catid For Columns id , catname For the column name, asmenu Parent to Column id When it is a top-level column, asmenu by 0  . 
protected function now_here($catid,$ext=''){
 $cat = M("Category");
 $here = '<a href="https://www.ofstack.com"> home page </a>';
 $uplevels = $cat->field("catid,catname,asmenu")->where("catid=$catid")->find();
 if($uplevels['asmenu'] != 0)
 $here .= $this->get_up_levels($uplevels['asmenu']);
 $here .= ' -&gt; <a href="/cat_'.$uplevels['catid'].'.html">'.$uplevels['catname']."</a>";
 if($ext != '') $here .= ' -&gt; '.$ext;
 return $here;
}
protected function get_up_levels($id){
 $cat = M("Category");
 $here = '';
 $uplevels = $cat->field("catid,catname,asmenu")->where("catid=$id")->find();
 $here .= ' -&gt; <a href="/cat_'.$uplevels['catid'].'.html">'.$uplevels['catname']."</a>";
 if($uplevels['asmenu'] != 0){
  $here = $this->get_up_levels($uplevels['asmenu']).$here;
 }
 return $here;
}


Appendix: Another example


class IndexAction extends Action {
    public function cat() {
  load('extend');  //  Load  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 classifications (and construct them into 1 Trees) 
  // $CategoryTree = list_to_tree($Categories, 'categories_id', 'parent_id');
    }
    /**
     *  By Classification id Backward construction of breadcrumbs 
     * @param  $Categories  An array of all classifications 
     * @param  $categoryId  Categories to be backtracked up id 
     * @param  $navCrumbs  Array to save results, passed in 1 Number of empty arrays is good 
     */
    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 ) {  //  There  0  Is Root Node id ( root node id ) 
      $this->getNavCrumbs($Categories, $parent_id, $navCrumbs);
     }
     $navCrumbs[$categories_id] = $category;
    }
}



Related articles: