1 <?php
2 /**
3 * 通用的树型类,可以生成任何树型结构
4 */
5 class tree {
6 /**
7 * 生成树型结构所需要的2维数组
8 * @var array
9 */
10 public $arr = array();
11
12 /**
13 * 生成树型结构所需修饰符号,可以换成图片
14 * @var array
15 */
16 public $icon = array('│','├','└');
17 public $nbsp = " ";
18
19 /**
20 * @access private
21 */
22 public $ret = '';
23
24 /**
25 * 构造函数,初始化类
26 * @param array 2维数组,例如:
27 * array(
28 * 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
29 * 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
30 * 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
31 * 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
32 * 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
33 * 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
34 * 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
35 * )
36 */
37 public function init($arr=array()){
38 $this->arr = $arr;
39 $this->ret = '';
40 return is_array($arr);
41 }
42
43 /**
44 * 得到父级数组
45 * @param int
46 * @return array
47 */
48 public function get_parent($myid){
49 $newarr = array();
50 if(!isset($this->arr[$myid])) return false;
51 $pid = $this->arr[$myid]['parentid'];
52 $pid = $this->arr[$pid]['parentid'];
53 if(is_array($this->arr)){
54 foreach($this->arr as $id => $a){
55 if($a['parentid'] == $pid) $newarr[$id] = $a;
56 }
57 }
58 return $newarr;
59 }
60
61 /**
62 * 得到子级数组
63 * @param int
64 * @return array
65 */
66 public function get_child($myid){
67 $a = $newarr = array();
68 if(is_array($this->arr)){
69 foreach($this->arr as $id => $a){
70 if($a['parentid'] == $myid) $newarr[$id] = $a;
71 }
72 }
73 return $newarr ? $newarr : false;
74 }
75
76 /**
77 * 得到当前位置数组
78 * @param int
79 * @return array
80 */
81 public function get_pos($myid,&$newarr){
82 $a = array();
83 if(!isset($this->arr[$myid])) return false;
84 $newarr[] = $this->arr[$myid];
85 $pid = $this->arr[$myid]['parentid'];
86 if(isset($this->arr[$pid])){
87 $this->get_pos($pid,$newarr);
88 }
89 if(is_array($newarr)){
90 krsort($newarr);
91 foreach($newarr as $v){
92 $a[$v['id']] = $v;
93 }
94 }
95 return $a;
96 }
97
98 /**
99 * 得到树型结构
100 * @param int ID,表示获得这个ID下的所有子级
101 * @param string 生成树型结构的基本代码,例如:"<option value=\$id \$selected>\$spacer\$name</option>"
102 * @param int 被选中的ID,比如在做树型下拉框的时候需要用到
103 * @return string
104 */
105 public function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = ''){
106 $number=1;
107 $child = $this->get_child($myid);
108 if(is_array($child)){
109 $total = count($child);
110 foreach($child as $id=>$a){
111 $j=$k='';
112 if($number==$total){
113 $j .= $this->icon[2];
114 }else{
115 $j .= $this->icon[1];
116 $k = $adds ? $this->icon[0] : '';
117 }
118 $spacer = $adds ? $adds.$j : '';
119 $selected = $id==$sid ? 'selected' : '';
120 @extract($a);
121 $parentid == 0 && $str_group ? eval("\$nstr = \"$str_group\";") : eval("\$nstr = \"$str\";");
122 $this->ret .= $nstr;
123 $nbsp = $this->nbsp;
124 $this->get_tree($id, $str, $sid, $adds.$k.$nbsp,$str_group);
125 $number++;
126 }
127 }
128 return $this->ret;
129 }
130 /**
131 * 同上一方法类似,但允许多选
132 */
133 public function get_tree_multi($myid, $str, $sid = 0, $adds = ''){
134 $number=1;
135 $child = $this->get_child($myid);
136 if(is_array($child)){
137 $total = count($child);
138 foreach($child as $id=>$a){
139 $j=$k='';
140 if($number==$total){
141 $j .= $this->icon[2];
142 }else{
143 $j .= $this->icon[1];
144 $k = $adds ? $this->icon[0] : '';
145 }
146 $spacer = $adds ? $adds.$j : '';
147
148 $selected = $this->have($sid,$id) ? 'selected' : '';
149 @extract($a);
150 eval("\$nstr = \"$str\";");
151 $this->ret .= $nstr;
152 $this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
153 $number++;
154 }
155 }
156 return $this->ret;
157 }
158 /**
159 * @param integer $myid 要查询的ID
160 * @param string $str 第一种HTML代码方式
161 * @param string $str2 第二种HTML代码方式
162 * @param integer $sid 默认选中
163 * @param integer $adds 前缀
164 */
165 public function get_tree_category($myid, $str, $str2, $sid = 0, $adds = ''){
166 $number=1;
167 $child = $this->get_child($myid);
168 if(is_array($child)){
169 $total = count($child);
170 foreach($child as $id=>$a){
171 $j=$k='';
172 if($number==$total){
173 $j .= $this->icon[2];
174 }else{
175 $j .= $this->icon[1];
176 $k = $adds ? $this->icon[0] : '';
177 }
178 $spacer = $adds ? $adds.$j : '';
179
180 $selected = $this->have($sid,$id) ? 'selected' : '';
181 @extract($a);
182 if (empty($html_disabled)) {
183 eval("\$nstr = \"$str\";");
184 } else {
185 eval("\$nstr = \"$str2\";");
186 }
187 $this->ret .= $nstr;
188 $this->get_tree_category($id, $str, $str2, $sid, $adds.$k.' ');
189 $number++;
190 }
191 }
192 return $this->ret;
193 }
194
195 /**
196 * 同上一类方法,jquery treeview 风格,可伸缩样式(需要treeview插件支持)
197 * @param $myid 表示获得这个ID下的所有子级
198 * @param $effected_id 需要生成treeview目录数的id
199 * @param $str 末级样式
200 * @param $style 目录样式 默认 filetree 可增加其他样式如'filetree treeview-famfamfam'
201 * @param $recursion 递归使用 外部调用时为FALSE
202 */
203 function get_treeview($myid,$effected_id='example',$str="<span class='file'>\$name</span>", $str2="<span class='folder'>\$name</span>" ,$style='filetree ' ,$recursion=FALSE) {
204 $child = $this->get_child($myid);
205 if(!defined('EFFECTED_INIT')){
206 $effected = ' id="'.$effected_id.'"';
207 define('EFFECTED_INIT', 1);
208 } else {
209 $effected = '';
210 }
211 if(!$recursion) $this->str .='<ul'.$effected.' class="'.$style.'">';
212 foreach($child as $id=>$a) {
213 @extract($a);
214 $floder_status = isset($folder) ? ' class="'.$folder.'"' : '';
215 $this->str .= $recursion ? '<ul><li'.$floder_status.'>' : '<li'.$floder_status.'>';
216 $recursion = FALSE;
217 if($this->get_child($id)){
218 eval("\$nstr = \"$str2\";");
219 $this->str .= $nstr;
220 $this->get_treeview($id,$effected_id,$str,$str2,$style,TRUE);
221 } else {
222 eval("\$nstr = \"$str\";");
223 $this->str .= $nstr;
224 }
225 $this->str .=$recursion ? '</li></ul>': '</li>';
226 }
227 if(!$recursion) $this->str .='</ul>';
228 return $this->str;
229 }
230 private function have($list,$item){
231 return(strpos(',,'.$list.',',','.$item.','));
232 }
233 }
234 ?>
http://bbs.phpchina.com/forum.php?mod=viewthread&tid=233878#lastpost

浙公网安备 33010602011771号