Home > Backend Development > PHP Tutorial > php infinite level SelectTree class_PHP tutorial

php infinite level SelectTree class_PHP tutorial

WBOY
Release: 2016-07-21 15:46:31
Original
822 people have browsed it

Copy code The code is as follows:

/*
author: nick
date: 2009.05.17
Function: Generate SeletTree
attributes:
$result result set
$id_field own id field
$parent_field parent class id field
$option_text option display name
$select_name name of drop-down menu
$elected is selected by default
$no_top whether top-level options are required
$level layer depth
$parent_id id in the same layer
*/
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();
}
/*
Function: Return a Tree two-dimensional array
*/
function getArray(){
$arrays=array();
while($row=mysql_fetch_array($this->result) ){
$arrays[$row[$this->parent_field]][$row[$this->id_field]]=$row;
}
return $arrays;
}
/*
Function: Get SelectTree
*/
function getSelectTree(){
$tree = '';
return $tree;
}
/*
Function: Recursively build tree structure
*/
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 .= ' '; //Option indent depth
foreach($array[$parent_id] as $key => ; $value){
if($value[$option_value] == $selected){
$tree .= '";
}else{
$tree .= '";
}
$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();


http://www.bkjia.com/PHPjc/320110.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320110.htmlTechArticleCopy the code as follows: /* author: nick date: 2009.05.17 Function: Generate SeletTree Attribute: $result result Set $id_field own id field $parent_field parent class id field $option_text option...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template