基于thinkphp框架站点导航

首先我在我的数据库 里面建有一个tasoo_node数据
相关字段是
nid :_pk
pid(父节点)
nodeText 节点名称
description 节点描述
后面我会依照这个数据库生成一个 SiteMap.xml的数据源文件 保存在 ./appName/runtime/data/文件夹下面
这个文件的格式如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <SiteMap>
  3.   <SiteMapNode url="url" title="title" description="des" nid="nid" pid="pid"/>
  4. <SiteMapNode url="url" title="title" description="des" nid="nid" pid="pid">
  5. <SiteMapNode url="url" title="title" description="des" nid="nid" pid="pid"/>
  6. .....
  7. </SiteMapNode>
  8. </SiteMap>
复制代码

总之就可以用这个文件描述树的结构
好 准备工作好了
首先建立树的类
类名: Tree.class .php 保存在ThinkPHP /lib/Com/CNTY/文件夹下
相关代码如下

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Tasoo all right resvered        
  4. // +----------------------------------------------------------------------
  5. // | 中国微型软件,电子商务系统
  6. // +----------------------------------------------------------------------
  7. // | 作者 Tasoo
  8. // +----------------------------------------------------------------------
  9. // | E-MAIL: Tasoo@163.com
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * Tree 实现
  15. +------------------------------------------------------------------------------
  16. * @category   Com
  17. * @package  Com
  18. * @subpackage  Tasoo
  19. * @author   Tasoo <Tasoo@163.com>
  20. * @version   $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Tree
  24. {
  25.     protected  $data    = array();
  26.     protected $child    = array(-1=>array());
  27.     protected $layer    = array(-1=>-1);
  28.     protected $parent    = array();
  29.     function __construct ($value)
  30.     {
  31.         $this->setNode(0, -1, $value);
  32.     } // end func
  33.     protected function setNode ($id, $parent, $value)
  34.     {
  35.         $parent = $parent?$parent:0;
  36.         $this->data[$id]            = $value;
  37.         $this->child[$id]            = array();
  38.         $this->child[$parent][]        = $id;
  39.         $this->parent[$id]            = $parent;
  40.         if (!isset($this->layer[$parent]))
  41.         {
  42.             $this->layer[$id] = 0;
  43.         }
  44.         else
  45.         {
  46.             $this->layer[$id] = $this->layer[$parent] + 1;
  47.         }
  48.     } // end func
  49.     protected function getList (&$tree, $root= 0)
  50.     {
  51.         foreach ($this->child[$root] as $key=>$id)
  52.         {
  53.             $tree[] = $id;
  54.             if ($this->child[$id]) $this->getList($tree, $id);
  55.         }
  56.     } // end func
  57.     protected function getValue ($id)
  58.     {
  59.         return $this->data[$id];
  60.     } // end func
  61.     protected function getLayer ($id, $space = false)
  62.     {
  63.         return $space?str_repeat($space, $this->layer[$id])this->layer[$id];
  64.     } // end func
  65.    protected function getParent ($id)
  66.     {
  67.         return $this->parent[$id];
  68.     } // end func
  69.     protected function getParents ($id)
  70.     {
  71.         while ($this->parent[$id] != -1)
  72.         {
  73.             $id = $parent[$this->layer[$id]] = $this->parent[$id];
  74.         }
  75.         ksort($parent);
  76.         reset($parent);
  77.         return $parent;
  78.     } // end func
  79.    
  80.     protected function getChilds ($id = 0)
  81.     {
  82.         $child = array($id);
  83.         $this->getList($child, $id);
  84.         return $child;
  85.     } // end func
  86. } // end class
  87. ?>
复制代码

现在介绍从数据库区数据然后生成SiteMap.xml文件的类
类名是: BuildSiteMap.class.php 保存在ThinkPHP/lib/Com/CNTY/文件夹下
相关代码是

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Tasoo all right resvered        
  4. // +----------------------------------------------------------------------
  5. // | Tasoo
  6. // +----------------------------------------------------------------------
  7. // | 作者 Tasoo
  8. // +----------------------------------------------------------------------
  9. // | E-MAIL:Tasoo@163.com
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * Tree 实现
  15. +------------------------------------------------------------------------------
  16. * @category  Com
  17. * @package  Com
  18. * @subpackage Tasoo
  19. * @author   Tasoo<Tasoo@163.com>
  20. * @version   $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. import ('Com.CNTY.Tree');
  24. class BuildSiteMap extends Tree{
  25.         private $nodeData=array();
  26.         private $nodeXML ;//这个用于递归方法保存节点XML,用于为建立siteMap.xml,
  27.         function __construct($data,$rootName){
  28.                 parent::__construct($rootName);
  29.                 $this->nodeData=$data;
  30.                 $this->init();
  31.         }
  32.         private function init(){
  33. /**
  34. * 这个函数 负责装入数据
  35. *
  36. */
  37.         foreach ($this->nodeData as $item){
  38.                         $this->setNode ($item['nid'],$item['pid'], $item['nodeText']);
  39.                 }                        
  40.         }
  41.         private function buildXML($root= 0){
  42.                 foreach ($this->child[$root] as $key=>$id){
  43.                         $this->nodeXML.="<SiteMapNode url=/"#/" title=/"{$this->getValue($id)}/" description=/"呵呵呵/" nid=/"$id/" pid=/"$root/">";
  44.             if ($this->child[$id]) $this->buildXML( $id);
  45.            $this->nodeXML.="</SiteMapNode>";
  46.         }
  47.         }
  48.         public function saveXML($filePath){
  49.                 if(!file_exists($filePath))
  50.                 return ;
  51.                 $this->nodeXML="<?xml version=/"1.0/" encoding=/"utf-8/"?><SiteMap>";
  52.                 $this->buildXML(-1);
  53.                 $this->nodeXML.="</SiteMap>";
  54.                 $xml=new  DOMDocument();
  55.                 $xml->formatOutput = true;
  56.                 $xml->preserveWhiteSpace = false;
  57.                 $xml->loadXML($this->nodeXML);
  58.                 $xml->save($filePath);
  59.         }
  60. }
  61. ?>
复制代码

这个类只在改变数据表tasoo_node才需要运行
这个一个函数就是解析 SiteMap.xml文件的函数
函数名: ShowPath($nid) 放在 ./appName/common/functions.php
相关代码:

  1. /**
  2. +----------------------------------------------------------
  3. * 显示站的路径
  4. +----------------------------------------------------------
  5. * @access public
  6. +----------------------------------------------------------
  7. * @param int $nid 栏目ID
  8. +----------------------------------------------------------
  9. * @return array
  10. +----------------------------------------------------------
  11. */
  12. function ShowPath($nid=-1){
  13.         $path=array();
  14.         $nid=$nid;
  15.         $SiteMapFile=DATA_PATH."SiteMap.xml";
  16.         $Dom=new DOMDocument();
  17.         $Dom->load($SiteMapFile);
  18.         $tasoo=$Dom->getElementsByTagName("SiteMapNode");
  19.         //dump($tasoo);
  20.         //foreach ($tasoo as $item){
  21.         //        dump($item->getAttribute('title'));
  22.         //}
  23.         $query=new DOMXPath($Dom);
  24.         $target=$query->query("//SiteMapNode[@nid=/"$nid/"]");
  25.         $target=$target->item(0);
  26.         //dump($target->parentNode);
  27.         while($target->parentNode){
  28.                 $path[]=array(
  29.                 'title'=>$target->getAttribute("title"),
  30.                 'nid'=>$target->getAttribute("nid"),
  31.                 'pid'=>$target->getAttribute("pid"),
  32.                 'description',$target->getAttribute("description"),
  33.                 'url'=>$target->getAttribute("url")
  34.                 );
  35.                 $target=$target->parentNode;
  36.         }
  37.         $path=array_reverse($path);
  38.         array_shift($path);
  39.         return $path;
  40. }
复制代码

这个函数通过解析xml文件 然后返回一个 array('title'=>title','nid'=>nid,.....);形式的数组
用于在标签 解析时候调用

在/ThinkPHP/Lib/Think/Template/Tags文件夹下建立 tasoo.xml
在/ThinkPHP/Lib/Think/Template//TagLib 建立相应的解析类 TagLibTasoo.class.php
tasoo.xml如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <taglib>
  3. <shortname>tasoo</shortname>
  4. <tag>
  5.         <name>GetPath</name>
  6.         <bodycontent>empty</bodycontent>
  7.         <attribute>
  8.                 <name>nid</name>
  9.                 <required>true</required>
  10.         </attribute>
  11. </tag>
  12. </taglib>
复制代码

TagLibTasoo.class.php 代码如下

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Tasoo all right resvered        
  4. // +----------------------------------------------------------------------
  5. // | Tasoo
  6. // +----------------------------------------------------------------------
  7. // | 作者 Tasoo
  8. // +----------------------------------------------------------------------
  9. // | E-MAIL: Tasoo@163.com
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. import('TagLib');
  13. /**
  14. +------------------------------------------------------------------------------
  15. * Tasoo标签库解析类
  16. +------------------------------------------------------------------------------
  17. * @category   Think
  18. * @package  Think
  19. * @subpackage  Template
  20. * @author   Tasoo<Tasoo@163.com>
  21. * @version   $Id$
  22. +------------------------------------------------------------------------------
  23. */
  24. class TagLibTasoo extends TagLib{
  25.    public function _GetPath($attr){
  26.                    $tag= $this->parseXmlAttr($attr,'GetPath');
  27.                    $nid=(int)$tag['nid'];
  28.                    $pathData=ShowPath($nid);
  29.                    $Html="<div><span>您当前的位置 : </span>/n";
  30.                    foreach($pathData as $item){
  31.                            $Html.=" <span><a href=/"{$item['url']}/">".$item['title']."</a><span>  >>/n";
  32.                    }
  33.                    $Html.="</div>";
  34.                    return $Html;
  35.    }
  36. }
  37. ?>
复制代码

呵呵 大功告成了 这后就可以在相应的模板 里这样调用
<tasoo:GetPath nid='num'/>
发个我自己运行的结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值