php无限分类可支持输出树状图实现代码
来源:程序员人生 发布时间:2013-12-04 12:12:41 阅读次数:3380次
这是一段可支持输出树状图的php无限分类实现代码,转自其它网站有需要了解的朋友可参考一下哦,数据库结构我们只要id,parentid,name这三个字段就可以了,大家可自行创建。
数据库结构我们只要,无平台限制,只需要告知id,parentid,name 即可,下面是php代码,需要php环境支持,代码如下:
- <?php
-
-
-
- class tree
- {
-
-
-
-
- var $arr = array();
-
-
-
-
- var $icon = array('│','├','└');
-
-
-
- var $ret = '';
-
-
-
-
-
-
-
-
-
-
-
-
-
- function tree($arr=array())
- {
- $this->arr = $arr;
- $this->ret = '';
- return is_array($arr);
- }
-
-
-
-
-
- function get_parent($myid)
- {
- $newarr = array();
- if(!isset($this->arr[$myid])) return false;
- $pid = $this->arr[$myid]['parentid'];
- $pid = $this->arr[$pid]['parentid'];
- if(is_array($this->arr))
- {
- foreach($this->arr as $id => $a)
- {
- if($a['parentid'] == $pid) $newarr[$id] = $a;
- }
- }
- return $newarr;
- }
-
-
-
-
-
- function get_child($myid)
- {
- $a = $newarr = array();
- if(is_array($this->arr))
- {
- foreach($this->arr as $id => $a)
- {
- if($a['parentid'] == $myid) $newarr[$id] = $a;
- }
- }
- return $newarr ? $newarr : false;
- }
-
-
-
-
-
- function get_pos($myid,&$newarr)
- {
- $a = array();
- if(!isset($this->arr[$myid])) return false;
- $newarr[] = $this->arr[$myid];
- $pid = $this->arr[$myid]['parentid'];
- if(isset($this->arr[$pid]))
- {
- $this->get_pos($pid,$newarr);
- }
- if(is_array($newarr))
- {
- krsort($newarr);
- foreach($newarr as $v)
- {
- $a[$v['id']] = $v;
- }
- }
- return $a;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '')
- {
- $number=1;
- $child = $this->get_child($myid);
- if(is_array($child))
- {
- $total = sqlserver/42852.htm target=_blank >count($child);
- foreach($child as $id=>$a)
- {
- $j=$k='';
- if($number==$total)
- {
- $j .= $this->icon[2];
- }
- else
- {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds.$j : '';
- $selected = $id==$sid ? 'selected' : '';
- @extract($a);
- $parentid == 0 && $str_group ? eval("$nstr = "$str_group";") : eval("$nstr = "$str";");
- $this->ret .= $nstr;
- $this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group);
- $number++;
- }
- }
- return $this->ret;
- }
-
-
-
- function get_tree_multi($myid, $str, $sid = 0, $adds = '')
- {
- $number=1;
- $child = $this->get_child($myid);
- if(is_array($child))
- {
- $total = count($child);
- foreach($child as $id=>$a)
- {
- $j=$k='';
- if($number==$total)
- {
- $j .= $this->icon[2];
- }
- else
- {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds.$j : '';
- $selected = $this->have($sid,$id) ? 'selected' : '';
-
- @extract($a);
- eval("$nstr = "$str";");
- $this->ret .= $nstr;
- $this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
- $number++;
- }
- }
- return $this->ret;
- }
- function have($list,$item){
- return(strpos(',,'.$list.',',','.$item.','));
- }
- }
- ?>
效果就是
aa
bbb
ccc 这样哦,可实现无限级分类.
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠