Php unlimited SelectTree class. Copy the code as follows: * author: nickdate: 2009.05.17 function: generate the SeletTree attribute: $ result set $ id_field id field $ parent_field parent class id field $ option_text option
The code is as follows:
/*
Author: nick
Date: 2009.05.17
Function: generate a SeletTree.
Attribute:
$ Result set
$ Id_field id field
$ Parent_field parent class id field
$ Option_text option display name
$ Select_name: name of the 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: returns a two-dimensional Tree 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 ='';If ($ no_top ){$ Tree. ='Top layer';}Self: buildTree ($ this-> getarray, & $ tree, $ this-> id_field, $ this-> option_text, $ this-> selected, $ this-> level, $ this-> parent_id); // Generate a tree structure$ Tree. ='';
Return $ tree;
}
/*
Function: recursively construct a 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. =''. $ 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 ();
Http://www.bkjia.com/PHPjc/320110.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320110.htmlTechArticle code is as follows:/* author: nick date: 2009.05.17 function: generate SeletTree property: $ result set $ id_field own id field $ parent_field parent class id field $ option_text option...