国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 汕头市| 德兴市| 全南县| 林周县| 涪陵区| 澳门| 兴业县| 二手房| 东丰县| 伊吾县| 乌兰浩特市| 根河市| 桑植县| 鲁甸县| 博罗县| 辉县市| 侯马市| 凉山| 福州市| 秦皇岛市| 林周县| 新津县| 镇赉县| 临夏市| 鄂州市| 山西省| 肃南| 涞源县| 华安县| 祁东县| 咸阳市| 平塘县| 准格尔旗| 宜良县| 乌兰察布市| 平遥县| 孝义市| 阿尔山市| 扶绥县| 贵州省| 怀仁县|