首页 > php教程 > php手册 > 正文

php 无限级 SelectTree 类

WBOY
发布: 2016-06-13 12:23:57
原创
738 人浏览过

复制代码 代码如下:


/*
author: nick
date: 2009.05.17
功能:生成SeletTree
属性:
$result 结果集
$id_field 自身id字段
$parent_field 父类id字段
$option_text 选项显示名称
$select_name 下拉菜单的名称
$elected 默认选中
$no_top 是否需要顶层选项
$level 层深度
$parent_id 同层中的id
*/
class SelectTree{
public $result;
public $select_name;
public $option_text;
public $elected;
public $id_field;
public $parent_field;
public $no_top;
public $level;
public $parent_id;
public $getarray;
function __construct($result,$id_field,$parent_field,$option_text,$select_name='',$elected=0,$no_top=0,$level=0,$parent_id=0){
$this->result =$result;
$this->id_field =$id_field;
$this->parent_field =$parent_field;
$this->option_text =$option_text;
$this->select_name =$select_name;
$this->elected =$elected;
$this->no_top =$no_top;
$this->level =$level;
$this->parent_id =$parent_id;
$this->getarray =self::getArray();
}
/*
功能:返回Tree二维数组
*/
function getArray(){
$arrays=array();
while($row=mysql_fetch_array($this->result)){
$arrays[$row[$this->parent_field]][$row[$this->id_field]]=$row;
}
return $arrays;
}
/*
功能:获取SelectTree
*/
function getSelectTree(){
$tree = '';
return $tree;
}
/*
功能:递归构建树状结构
*/
function buildTree($array,&$tree,$option_value,$option_text,$selected,$level=0,$parent_id=0){
if(is_array($array[$parent_id])){
for($i=0;$i$space .= ' '; //选项缩进深度
foreach($array[$parent_id] as $key => $value){
if($value[$option_value] == $selected){
$tree .= '";
}else{
$tree .= '";
}
$tree .=self::buildTree($array,&$tree,$option_value,$option_text,$selected,$level+1,$key);
}
}else{
$tree .= '';
}
}
}
/****************************************************************************/
header("CONTENT-TYPE:TEXT/HTML;CHARSET=UTF-8");
mysql_connect("localhost","root","root");
mysql_select_db("tree");
mysql_query('set names utf8');
$result = mysql_query("select * from tvmenu");
$tree=new SelectTree($result,'id','bid','name','tree');
echo $tree->getSelectTree();

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!