首先我在我的数据库 里面建有一个tasoo_node数据 表
相关字段是
nid :_pk
pid(父节点)
nodeText 节点名称
description 节点描述
后面我会依照这个数据库生成一个 SiteMap.xml的数据源文件 保存在 ./appName/runtime/data/文件夹下面
这个文件的格式如下
- <?xml version="1.0" encoding="utf-8"?>
- <SiteMap>
- <SiteMapNode url="url" title="title" description="des" nid="nid" pid="pid"/>
- <SiteMapNode url="url" title="title" description="des" nid="nid" pid="pid">
- <SiteMapNode url="url" title="title" description="des" nid="nid" pid="pid"/>
- .....
- </SiteMapNode>
- </SiteMap>
总之就可以用这个文件描述树的结构
好 准备工作好了
首先建立树的类
类名: Tree.class .php 保存在ThinkPHP /lib/Com/CNTY/文件夹下
相关代码如下
- <?php
- // +----------------------------------------------------------------------
- // | Tasoo all right resvered
- // +----------------------------------------------------------------------
- // | 中国微型软件,电子商务系统
- // +----------------------------------------------------------------------
- // | 作者 Tasoo
- // +----------------------------------------------------------------------
- // | E-MAIL: Tasoo@163.com
- // +----------------------------------------------------------------------
- // $Id$
- /**
- +------------------------------------------------------------------------------
- * Tree 实现
- +------------------------------------------------------------------------------
- * @category Com
- * @package Com
- * @subpackage Tasoo
- * @author Tasoo <Tasoo@163.com>
- * @version $Id$
- +------------------------------------------------------------------------------
- */
- class Tree
- {
- protected $data = array();
- protected $child = array(-1=>array());
- protected $layer = array(-1=>-1);
- protected $parent = array();
- function __construct ($value)
- {
- $this->setNode(0, -1, $value);
- } // end func
- protected function setNode ($id, $parent, $value)
- {
- $parent = $parent?$parent:0;
- $this->data[$id] = $value;
- $this->child[$id] = array();
- $this->child[$parent][] = $id;
- $this->parent[$id] = $parent;
- if (!isset($this->layer[$parent]))
- {
- $this->layer[$id] = 0;
- }
- else
- {
- $this->layer[$id] = $this->layer[$parent] + 1;
- }
- } // end func
- protected function getList (&$tree, $root= 0)
- {
- foreach ($this->child[$root] as $key=>$id)
- {
- $tree[] = $id;
- if ($this->child[$id]) $this->getList($tree, $id);
- }
- } // end func
- protected function getValue ($id)
- {
- return $this->data[$id];
- } // end func
- protected function getLayer ($id, $space = false)
- {
- return $space?str_repeat($space, $this->layer[$id])this->layer[$id];
- } // end func
- protected function getParent ($id)
- {
- return $this->parent[$id];
- } // end func
- protected function getParents ($id)
- {
- while ($this->parent[$id] != -1)
- {
- $id = $parent[$this->layer[$id]] = $this->parent[$id];
- }
- ksort($parent);
- reset($parent);
- return $parent;
- } // end func
- protected function getChilds ($id = 0)
- {
- $child = array($id);
- $this->getList($child, $id);
- return $child;
- } // end func
- } // end class
- ?>
现在介绍从数据库区数据然后生成SiteMap.xml文件的类
类名是: BuildSiteMap.class.php 保存在ThinkPHP/lib/Com/CNTY/文件夹下
相关代码是
- <?php
- // +----------------------------------------------------------------------
- // | Tasoo all right resvered
- // +----------------------------------------------------------------------
- // | Tasoo
- // +----------------------------------------------------------------------
- // | 作者 Tasoo
- // +----------------------------------------------------------------------
- // | E-MAIL:Tasoo@163.com
- // +----------------------------------------------------------------------
- // $Id$
- /**
- +------------------------------------------------------------------------------
- * Tree 实现
- +------------------------------------------------------------------------------
- * @category Com
- * @package Com
- * @subpackage Tasoo
- * @author Tasoo<Tasoo@163.com>
- * @version $Id$
- +------------------------------------------------------------------------------
- */
- import ('Com.CNTY.Tree');
- class BuildSiteMap extends Tree{
- private $nodeData=array();
- private $nodeXML ;//这个用于递归方法保存节点XML,用于为建立siteMap.xml,
- function __construct($data,$rootName){
- parent::__construct($rootName);
- $this->nodeData=$data;
- $this->init();
- }
- private function init(){
- /**
- * 这个函数 负责装入数据
- *
- */
- foreach ($this->nodeData as $item){
- $this->setNode ($item['nid'],$item['pid'], $item['nodeText']);
- }
- }
- private function buildXML($root= 0){
- foreach ($this->child[$root] as $key=>$id){
- $this->nodeXML.="<SiteMapNode url=/"#/" title=/"{$this->getValue($id)}/" description=/"呵呵呵/" nid=/"$id/" pid=/"$root/">";
- if ($this->child[$id]) $this->buildXML( $id);
- $this->nodeXML.="</SiteMapNode>";
- }
- }
- public function saveXML($filePath){
- if(!file_exists($filePath))
- return ;
- $this->nodeXML="<?xml version=/"1.0/" encoding=/"utf-8/"?><SiteMap>";
- $this->buildXML(-1);
- $this->nodeXML.="</SiteMap>";
- $xml=new DOMDocument();
- $xml->formatOutput = true;
- $xml->preserveWhiteSpace = false;
- $xml->loadXML($this->nodeXML);
- $xml->save($filePath);
- }
- }
- ?>
这个类只在改变数据表tasoo_node才需要运行
这个一个函数就是解析 SiteMap.xml文件的函数
函数名: ShowPath($nid) 放在 ./appName/common/functions.php
相关代码:
- /**
- +----------------------------------------------------------
- * 显示站的路径
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param int $nid 栏目ID
- +----------------------------------------------------------
- * @return array
- +----------------------------------------------------------
- */
- function ShowPath($nid=-1){
- $path=array();
- $nid=$nid;
- $SiteMapFile=DATA_PATH."SiteMap.xml";
- $Dom=new DOMDocument();
- $Dom->load($SiteMapFile);
- $tasoo=$Dom->getElementsByTagName("SiteMapNode");
- //dump($tasoo);
- //foreach ($tasoo as $item){
- // dump($item->getAttribute('title'));
- //}
- $query=new DOMXPath($Dom);
- $target=$query->query("//SiteMapNode[@nid=/"$nid/"]");
- $target=$target->item(0);
- //dump($target->parentNode);
- while($target->parentNode){
- $path[]=array(
- 'title'=>$target->getAttribute("title"),
- 'nid'=>$target->getAttribute("nid"),
- 'pid'=>$target->getAttribute("pid"),
- 'description',$target->getAttribute("description"),
- 'url'=>$target->getAttribute("url")
- );
- $target=$target->parentNode;
- }
- $path=array_reverse($path);
- array_shift($path);
- return $path;
- }
这个函数通过解析xml文件 然后返回一个 array('title'=>title','nid'=>nid,.....);形式的数组
用于在标签 解析时候调用
在/ThinkPHP/Lib/Think/Template/Tags文件夹下建立 tasoo.xml
在/ThinkPHP/Lib/Think/Template//TagLib 建立相应的解析类 TagLibTasoo.class.php
tasoo.xml如下
- <?xml version="1.0" encoding="utf-8"?>
- <taglib>
- <shortname>tasoo</shortname>
- <tag>
- <name>GetPath</name>
- <bodycontent>empty</bodycontent>
- <attribute>
- <name>nid</name>
- <required>true</required>
- </attribute>
- </tag>
- </taglib>
TagLibTasoo.class.php 代码如下
- <?php
- // +----------------------------------------------------------------------
- // | Tasoo all right resvered
- // +----------------------------------------------------------------------
- // | Tasoo
- // +----------------------------------------------------------------------
- // | 作者 Tasoo
- // +----------------------------------------------------------------------
- // | E-MAIL: Tasoo@163.com
- // +----------------------------------------------------------------------
- // $Id$
- import('TagLib');
- /**
- +------------------------------------------------------------------------------
- * Tasoo标签库解析类
- +------------------------------------------------------------------------------
- * @category Think
- * @package Think
- * @subpackage Template
- * @author Tasoo<Tasoo@163.com>
- * @version $Id$
- +------------------------------------------------------------------------------
- */
- class TagLibTasoo extends TagLib{
- public function _GetPath($attr){
- $tag= $this->parseXmlAttr($attr,'GetPath');
- $nid=(int)$tag['nid'];
- $pathData=ShowPath($nid);
- $Html="<div><span>您当前的位置 : </span>/n";
- foreach($pathData as $item){
- $Html.=" <span><a href=/"{$item['url']}/">".$item['title']."</a><span> >>/n";
- }
- $Html.="</div>";
- return $Html;
- }
- }
- ?>
呵呵 大功告成了 这后就可以在相应的模板 里这样调用
<tasoo:GetPath nid='num'/>
发个我自己运行的结果