php专区

 首页 > php专区 > PHP应用 > php类库 > php树型菜单类 - php类库

php树型菜单类 - php类库

分享到:
【字体:大 中 小】
导读:
          原理简单,学过数据结构的一看就明白是什么道理了,不过今天在使用时数据中出现了子节点id(71)小于父节点id(104) 导致部分子节点没被存储入数组,修改了一下,实例代码如下:?phpclasstree{v...

php树型菜单类

原理简单,学过数据结构的一看就明白是什么道理了,不过今天在使用时数据中出现了子节点id(71)小于父节点id(104).导致部分子节点没被存储入数组,修改了一下,实例代码如下:

  1. class tree 
  2. { 
  3.     var $data = array(); 
  4.     var $child = array(-1=>array()); 
  5.     var $layer = array(-1=>-1); 
  6.     var $parent = array(); 
  7.     var $num = array(); 
  8.   
  9.     function setnode($id, $parent, $value,$num=0) 
  10.     { 
  11.         $parent = $parent ? $parent : 0; 
  12.   
  13.         $this->data[$id]  = $value; 
  14.         $this->num[$id]      = $num; 
  15.         if (!isset($this->child[$id])) $this->child[$id] = array(); 
  16.         $this->child[$parent][] = $id; 
  17.         $this->parent[$id]  = $parent; 
  18.   
  19.         if (!isset($this->layer[$parent]) && $parent == 0) 
  20.         { 
  21.            $this->layer[$id] = 0; 
  22.         } 
  23.         else 
  24.         { 
  25.             $this->layer[$id] = $this->layer[$parent] + 1; 
  26.         } 
  27.     } 
  28.   
  29.   
  30.     function getlist(&$tree, $root= 0) 
  31.     { 
  32.         foreach ($this->child[$root] as $key=>$id) 
  33.         { 
  34.             $tree[] = $id; 
  35.             if($this->child[$id]) $this->getlist($tree, $id); 
  36.         } 
  37.     } 
  38.   
  39.     function getvalue($id) 
  40.     { 
  41.   if($this->layer[$id]==0) 
  42.   { 
  43.    return $this->data[$id]; 
  44.   } 
  45.   else 
  46.   { 
  47.    return $leftmar.$this->data[$id]; 
  48.   } 
  49.     } 
  50.   
  51.     function getnum($id) 
  52.     { 
  53.   return $this->num[$id]; 
  54.     } 
  55.   
  56.     function getbitvalue($id) 
  57.     { 
  58.   return $this->data[$id]; 
  59.     } 
  60.   
  61.     function getlayer($id, $space = false) 
  62.     { 
  63.         return $space ? str_repeat($space, $this->layer[$id]) : $this->layer[$id]; 
  64.     } 
  65.   
  66.     function getparent($id) 
  67.     { 
  68.         return $this->parent[$id]; 
  69.     } 
  70.   
  71.     function getparents($id) 
  72.     { 
  73.         while ($this->parent[$id] != -1) 
  74.         { 
  75.             $id = $parent[$this->layer[$id]] = $this->parent[$id]; 
  76.         } 
  77.   
  78.         ksort($parent); 
  79.         reset($parent); 
  80.   
  81.         return $parent; 
  82.     } 
  83.   
  84.     function getchild($id) 
  85.     { 
  86.         return $this->child[$id]; 
  87.     } 
  88.   
  89.     function getchilds($id = 0) 
  90.     { 
  91.         $child = array($id); 
  92.         $this->getlist($child, $id); 
  93.   
  94.         return $child; 
  95.     } 
  96.   
  97.     function printdata() 
  98.     { 
  99.         return $this->layer; 
  100.     } 
  101. } 
  102. ?> 
分享到:
php中文目录操作类 - php类库
php中文目录操作类 下面这个文件操作类可以建立目录,删除目录,删除文件等一系列你能操作操作的功能,代码如下:   
php ExcelReader读取excel文件 - php类库
php ExcelReader读取excel文件 一、 概述   php-excelreader 是一个读取 excel xsl 文件内容的一个php类,它的下载网址:http://sourceforge.net/projects/phpexcelreader/ 测试用excel文件:测试.xls,文件名:phpexcelreader.zip 包含两个必需文件:oleread.inc,reader.php,其它文件是一个应...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……