Home  >  Article  >  php教程  >  thinkphp获取栏目和文章当前位置的方法

thinkphp获取栏目和文章当前位置的方法

WBOY
WBOYOriginal
2016-06-06 20:18:431232browse

这篇文章主要介绍了thinkphp获取栏目和文章当前位置的方法,通过一个自定义的递归函数读取目录来实现获取栏目和文章当前位置,是非常实用的技巧,需要的朋友可以参

本文实例讲述了thinkphp获取栏目和文章当前位置的方法。分享给大家供大家参考。具体实现方法如下:

今天把博客一些细节完善了一下,其中修改了一下栏目页和文章页中的“当前位置”。以前栏目很少,就用死办法做的(首页 -> 栏目的名字),现在栏目多了,渐渐二级栏目,三级栏目也来了,这样的方式显然不太合适,于是就改进了一下。也不难,利用一个递归函数就可以了。

测试效果如下图所示:

查看源文件效果:

复制代码 代码如下:

首页 -> PHP学习 -> ecshop -> ecshop二次开发 -> ecshop加入百度地图,支持周边标记

复制代码 代码如下:

//当前位置-第一个参数 catid为当前栏目的id,第二个参数为文章的标题,,调用栏目当前位置时第二个参数为空即可。
$this->assign("now_here",$this->now_here($catid,$res['title']));
 
//解释一下,栏目表category中的catid为栏目id,catname为栏目名称,asmenu为栏目父级的id,当为顶级栏目时,asmenu为0 。

protected function now_here($catid,$ext=''){
 $cat = M("Category");
 $here = '首页';
 $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;
}

希望本文所述对大家的PHP程序设计有所帮助。

Statement:
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