php 无限级 SelectTree 类
2015-01-24信息快讯网
无限级 SelectTree php类实现代码。
/*
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 = '<select name="'.$this->select_name.'">';
if($no_top){
$tree .= '<option value="0">最顶层</option>';
}
self::buildTree($this->getarray,&$tree,$this->id_field,$this->option_text,$this->selected,$this->level,$this->parent_id); //生成树状结构
$tree .= '</select>';
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<$level;$i++)
$space .= ' '; //选项缩进深度
foreach($array[$parent_id] as $key => $value){
if($value[$option_value] == $selected){
$tree .= '<option value="'.$value[$option_value].'" selected="selected">'.$space.$value[$option_text]."</option>";
}else{
$tree .= '<option value="'.$value[$option_value].'">'.$space.$value[$option_text]."</option>";
}
$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();
Wordpress 相册插件 NextGEN-Gallery 添加目录将中文转为拼音的解决办法
php foreach 参数强制类型转换的问题
php iconv() : Detected an illegal character in input string
php运行出现Call to undefined function curl_init()的解决方法
POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
php表单转换textarea换行符的方法
PHP获取表单textarea数据中的换行问题
使用bcompiler对PHP文件进行加密的代码
PHP生成excel时单元格内换行问题的解决方法
php visitFile()遍历指定文件夹函数
php excel类 phpExcel使用方法介绍
php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)
ThinkPHP中实例Model方法的区别说明
php操作excel文件 基于phpexcel
PHP extract 将数组拆分成多个变量的函数
IIS下PHP连接数据库提示mysql undefined function mysql_connect()
php select,radio和checkbox默认选择的实现方法
PHP 数组入门教程小结
PHP日期时间函数的高级应用技巧
php反弹shell实现代码
一个php导出oracle库的php代码
php一句话cmdshell新型 (非一句话木马)
对squid中refresh_pattern的一些理解和建议
php SQL之where语句生成器
php array_intersect()函数使用代码
Google PR查询接口checksum新算法第1/2页
PHP Google的translate API代码
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
PHPMailer安装方法及简单实例
php下HTTP Response中的Chunked编码实现方法
PHP syntax error, unexpected $end 错误的一种原因及解决
PHP中str_replace函数使用小结
使用eAccelerator加密PHP程序
smarty section简介与用法分析
PHP Smarty生成EXCEL文档的代码
php-accelerator网站加速PHP缓冲的方法
PHP中的extract的作用分析
如何提高MYSQL数据库的查询统计速度 select 索引应用