国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php樹型菜單類

2024-09-04 11:44:04
字體:
來源:轉載
供稿:網友

原理簡單,學過數據結構的一看就明白是什么道理了,不過今天在使用時數據中出現(xiàn)了子節(jié)點id(71)小于父節(jié)點id(104).導致部分子節(jié)點沒被存儲入數組,修改了一下,實例代碼如下:

  1. <?php 
  2. class tree 
  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[$rootas $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. ?> 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 甘洛县| 苗栗市| 延庆县| 柏乡县| 上栗县| 曲靖市| 南投县| 乌恰县| 青州市| 伊通| 洮南市| 淮安市| 卫辉市| 建昌县| 正镶白旗| 调兵山市| 抚顺市| 铁岭县| 惠水县| 宜都市| 绥芬河市| 彭阳县| 林州市| 阿拉善右旗| 台南市| 新沂市| 开平市| 博乐市| 通许县| 新巴尔虎左旗| 瑞丽市| 江达县| 高雄县| 乃东县| 潮州市| 雷州市| 乐都县| 宁乡县| 贵南县| 灵武市| 武陟县|