php 无限级 SelectTree 类

yipeiwu_com5年前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();

相关文章

PHP中一个有趣的preg_replace函数详解

PHP中一个有趣的preg_replace函数详解

0x01 起因 事情的起因是下午遇到了 preg_replace 函数,我们都知道 preg_replace 函数可能会导致命令执行。现在我们来一些情况。 0x02 经过 踩坑1:...

PHP中substr函数字符串截取用法分析

本文实例讲述了PHP中substr函数字符串截取用法。分享给大家供大家参考,具体如下: PHP中substr函数定义如下: substr(string,start,length) 参数说...

PHP简单实现二维数组的矩阵转置操作示例

PHP简单实现二维数组的矩阵转置操作示例

本文实例讲述了PHP简单实现二维数组的矩阵转置操作。分享给大家供大家参考,具体如下: <?php $arr1 = array( array(1,2,...

php设计模式之简单工厂模式详解

php设计模式之简单工厂模式详解

本文以实例形式较为详细的介绍了PHP设计模式的简单工厂模式,对于进行PHP程序设计来说有很好的借鉴作用。具体如下: 一、概念 简单工厂模式 【静态工厂方法模式】(Static Facto...

php识别翻转iphone拍摄的颠倒图片

用iphone横向拍摄并上传的图片往往是向左或向右90度侧向显示的,本文介绍如何用php识别并且翻转图片到正确位置。 ps : 此方法只能判断一些手机相机拍摄的图片位置颠倒 ...