[备忘]一个树形类(修正了子节点ID小于父节点ID时的BUG)(1 位领导批示)
- 2009-03-25
- 分类:PHP&MySql
- 作者:银子
- 693 位领导视察
这个树形类还是相当好用..
不过今天在使用时数据中出现了子节点id(71)小于父节点id(104).导致部分子节点没被存储入数组
修改了一下..应该没有问题了..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | <?php class Tree { var $data = array(); var $child = array(-1=>array()); var $layer = array(-1=>-1); var $parent = array(); var $num = array(); function setNode($id, $parent, $value,$num=0) { $parent = $parent ? $parent : 0; $this->data[$id] = $value; $this->num[$id] = $num; if (!isset($this->child[$id])) $this->child[$id] = array(); $this->child[$parent][] = $id; $this->parent[$id] = $parent; if (!isset($this->layer[$parent]) && $parent == 0) { $this->layer[$id] = 0; } else { $this->layer[$id] = $this->layer[$parent] + 1; } } function getList(&$tree, $root= 0) { foreach ($this->child[$root] as $key=>$id) { $tree[] = $id; if($this->child[$id]) $this->getList($tree, $id); } } function getValue($id) { if($this->layer[$id]==0) { return $this->data[$id]; } else { return $leftmar.$this->data[$id]; } } function getNum($id) { return $this->num[$id]; } function getbitValue($id) { return $this->data[$id]; } function getLayer($id, $space = false) { return $space ? str_repeat($space, $this->layer[$id]) : $this->layer[$id]; } function getParent($id) { return $this->parent[$id]; } function getParents($id) { while ($this->parent[$id] != -1) { $id = $parent[$this->layer[$id]] = $this->parent[$id]; } ksort($parent); reset($parent); return $parent; } function getChild($id) { return $this->child[$id]; } function getChilds($id = 0) { $child = array($id); $this->getList($child, $id); return $child; } function printData() { return $this->layer; } } ?> |
















