Php unlimited SelectTree class

Source: Internet
Author: User

Copy codeThe 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 = '<select name = "'. $ this-> select_name. '"> ';
If ($ no_top ){
$ Tree. = '<option value = "0"> top layer </option> ';
}
Self: buildTree ($ this-> getarray, & $ tree, $ this-> id_field, $ this-> option_text, $ this-> selected, $ this-> level, $ this-> parent_id); // generate a Tree Structure
$ Tree. = '</select> ';
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. = '<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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.