色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php 無限級 SelectTree 類

復(fù)制代碼 代碼如下:
/*
author: nick
date: 2009.05.17
功能:生成SeletTree
屬性:
$result 結(jié)果集
$id_field 自身id字段
$parent_field 父類id字段
$option_text 選項(xiàng)顯示名稱
$select_name 下拉菜單的名稱
$elected 默認(rèn)選中
$no_top 是否需要頂層選項(xiàng)
$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二維數(shù)組
*/
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); //生成樹狀結(jié)構(gòu)
$tree .= '</select>';
return $tree;
}
/*
功能:遞歸構(gòu)建樹狀結(jié)構(gòu)
*/
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 .= ' '; //選項(xiàng)縮進(jìn)深度
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技術(shù)php 無限級 SelectTree 類,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 昌都县| 云浮市| 四会市| 安图县| 仁布县| 海伦市| 离岛区| 绥棱县| 临安市| 大方县| 花垣县| 孝感市| 荆州市| 平邑县| 北川| 莒南县| 武夷山市| 常山县| 七台河市| 库伦旗| 泾川县| 改则县| 嘉祥县| 新平| 东安县| 泊头市| 石阡县| 拜泉县| 紫云| 定远县| 荆门市| 红原县| 甘德县| 怀安县| 金华市| 固阳县| 迭部县| 南木林县| 北辰区| 虹口区| 奉化市|