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