php专区

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

php树型菜单类 - php类库

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

php树型菜单类

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

  1. class tree 
  2.     var $data = array(); 
  3.     var $child = array(-1=>array()); 
  4.     var $layer = array(-1=>-1); 
  5.     var $parent = array(); 
  6.     var $num = array(); 
  7.   
  8.     function setnode($id$parent$value,$num=0) 
  9.     { 
  10.         $parent = $parent ? $parent : 0; 
  11.   
  12.         $this->data[$id]  = $value
  13.         $this->num[$id]      = $num
  14.         if (!isset($this->child[$id])) $this->child[$id] = array(); 
  15.         $this->child[$parent][] = $id
  16.         $this->parent[$id]  = $parent
  17.   
  18.         if (!isset($this->layer[$parent]) && $parent == 0) 
  19.         { 
  20.            $this->layer[$id] = 0; 
  21.         } 
  22.         else 
  23.         { 
  24.             $this->layer[$id] = $this->layer[$parent] + 1; 
  25.         } 
  26.     } 
  27.   
  28.   
  29.     function getlist(&$tree$root= 0) 
  30.     { 
  31.         foreach ($this->child[$rootas $key=>$id
  32.         { 
  33.             $tree[] = $id
  34.             if($this->child[$id]) $this->getlist($tree$id); 
  35.         } 
  36.     } 
  37.   
  38.     function getvalue($id
  39.     { 
  40.   if($this->layer[$id]==0) 
  41.   { 
  42.    return $this->data[$id]; 
  43.   } 
  44.   else 
  45.   { 
  46.    return $leftmar.$this->data[$id]; 
  47.   } 
  48.     } 
  49.   
  50.     function getnum($id
  51.     { 
  52.   return $this->num[$id]; 
  53.     } 
  54.   
  55.     function getbitvalue($id
  56.     { 
  57.   return $this->data[$id]; 
  58.     } 
  59.   
  60.     function getlayer($id$space = false) 
  61.     { 
  62.         return $space ? str_repeat($space$this->layer[$id]) : $this->layer[$id]; 
  63.     } 
  64.   
  65.     function getparent($id
  66.     { 
  67.         return $this->parent[$id]; 
  68.     } 
  69.   
  70.     function getparents($id
  71.     { 
  72.         while ($this->parent[$id] != -1) 
  73.         { 
  74.             $id = $parent[$this->layer[$id]] = $this->parent[$id]; 
  75.         } 
  76.   
  77.         ksort($parent); 
  78.         reset($parent); 
  79.   
  80.         return $parent
  81.     } 
  82.   
  83.     function getchild($id
  84.     { 
  85.         return $this->child[$id]; 
  86.     } 
  87.   
  88.     function getchilds($id = 0) 
  89.     { 
  90.         $child = array($id); 
  91.         $this->getlist($child$id); 
  92.   
  93.         return $child
  94.     } 
  95.   
  96.     function printdata() 
  97.     { 
  98.         return $this->layer; 
  99.     } 
  100. ?> 
分享到:
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技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……