tree.class.php,php写的无限级selectTree类

/*

author: nick

功能:生成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 = '';

if($no_top){

$tree .= '最顶层';

}

self::buildTree($this->getarray,&$tree,$this->id_field,$this->option_text,$this->selected,$this->level,$this->parent_id); //生成树状结构

$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 .= ''.$space.$value[$option_text]."";

}else{

$tree .= ''.$space.$value[$option_text]."";

}

$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();

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值