Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to output the data of an unlimited classification table into a tree structure.
/**
* Function to obtain the tree structure. Note: callback and level parameters do not allow value passing.
* @ Param $ model string table
* @ Param $ id int current ID
* @ Param $ fields string the first three fields of the returned field correspond to the id, parent id, and name must contain at least the first three fields. The returned fullname is the formatted name, if the fullname field already exists in the table, we recommend that you use aliases.
* @ Param $ condition max query condition
* @ Param $ orderby string sorting
* @ Param $ self boolean whether the current ID data is included
* @ Param $ onlyson boolean whether only the lower-level is returned
* @ Param $ return string indicates the data type returned. ids indicates that only the id set tree is returned and tree data is returned.
* @ Return $ data returned by datas array
*/
Protected function treeStructure ($ model = '', $ id = 0, $ fields ='', $ condition = '', $ orderby ='', $ self = true, $ onlyson = false, $ return = 'tree', $ callback = false, $ level = 0 ){
// Verify the parameter type
If (! Is_string ($ model) |! Is_int ($ id) |! Is_string ($ fields) |! (Is_string ($ condition) | is_array ($ condition) |! Is_string ($ orderby) |! Is_bool ($ self) |! Is_bool ($ onlyson) |! In_array ($ return, array ('id', 'tree') return array ();
// Verify the parameter value
$ Fields_arr = explode (',', $ fields );
If (empty ($ model) | empty ($ fields) | count ($ fields_arr) <3) return array ();
// Obtain the fields in the table corresponding to the id, parent id, and name.
Foreach ($ fields_arr as $ k => $ f ){
$ F = trim ($ f );
$ F_arr = explode ('as', $ f );
$ F_arr [0] = trim ($ f_arr [0]);
$ F_arr [1] = trim ($ f_arr [1]);
$ Fields_arr [$ k] = array ('field' => $ f_arr [0], 'Alias' => $ f_arr [1]);
If ($ k = 2) break;
}
$ Field_id = $ fields_arr [0] ['field'];
$ Field_pid = $ fields_arr [1] ['field'];
$ Field_name = $ fields_arr [2] ['field'];
$ Model = strtolower ($ model );
// Query Conditions
If (! Empty ($ condition )){
If (is_array ($ condition) $ map = $ map1 = $ map2 = $ condition;
If (is_string ($ condition) $ map ['_ string'] = $ map1 [' _ string'] = $ map2 ['_ string'] = $ condition;
}
$ Map [$ field_pid] = $ id;
// Query
If (empty ($ orderby )){
$ List = M ()-> table ($ model)-> field ($ fields)-> where ($ map)-> select ();
} Else {
$ List = M ()-> table ($ model)-> field ($ fields)-> where ($ map)-> order ($ orderby)-> select ();
}
$ Datas = array ();
// Temporary field variable
$ Field_level = to_guid_string ('level ');
$ Field_last = to_guid_string ('last ');
$ Field_son_num = to_guid_string ('son _ num ');
$ Field_all_son_num = to_guid_string ('all _ son_num ');
If (! $ Callback & $ self & $ id! = 0 ){
$ Map1 [$ field_id] = $ id;
$ Info = M ()-> table ($ model)-> field ($ fields)-> where ($ map1)-> find ();
$ Info [$ field_level] = $ level;
$ Info [$ field_last] = 1;
$ Info [$ field_son_num] = count ($ list );
$ Datas [] = $ info;
$ Level ++;
}
If ($ onlyson ){
Foreach ($ list as $ k => $ v ){
$ V [$ field_level] = $ level;
$ V [$ field_last] = (count ($ list)-1) = $ k? 1:0;
$ V [$ field_son_num] = 0;
If ($ level = 0) $ info [$ field_all_son_num] = 0;
$ Datas [] = $ v;
}
} Else {
If (count ($ list)> 0 ){
Foreach ($ list as $ k => $ v ){
$ V [$ field_level] = $ level;
$ V [$ field_last] = (count ($ list)-1) = $ k? 1:0;
$ Map2 [$ field_pid] = $ v [$ field_id];
$ V [$ field_son_num] = M ()-> table ($ model)-> where ($ map2)-> count (); // obtain the number of sublevels
$ Next_id = intval ($ v [$ field_id]);
$ Next_level = $ level + 1;
$ Datasx = $ this-> treeStructure ($ model, $ next_id, $ fields, $ condition, $ orderby, true, false, $ return, true, $ next_level );
If ($ level = 0 ){
$ V [$ field_all_son_num] = count ($ datasx); // obtain the number of all sublevels
}
$ Datas [] = $ v;
If (! Empty ($ datasx )){
Foreach ($ datasx as $ v1 ){
$ Datas [] = $ v1;
}
}
}
}
}
If (! $ Callback & $ self & $ id! = 0 ){
$ Datas [0] [$ field_all_son_num] = count ($ datas)-1;
}
If (! $ Callback ){
$ Ids = array ();
$ All_son_num = 0;
Foreach ($ datas as $ k => $ v ){
$ Ids [] = $ v [$ field_id];
If ($ v [$ field_level] = 0 ){
$ All_son_num = $ v [$ field_all_son_num];
$ I = 0;
$ V ['fullname'] = $ v [$ field_name];
} Else {
$ I ++;
$ Prev_num = $ v [$ field_level]-1;
If ($ prev_num> 0 ){
$ Prev_icon_t = $ I = $ all_son_num? 'Hangzhou': '│ ';
$ Prev_icon = str_repeat (''. $ prev_icon_t, $ prev_num );
} Else {
$ Prev_icon = '';
}
$ Icon = $ v [$ field_last] & $ v [$ field_son_num] = 0? 'Handler': 'handler ';
$ V ['fullname'] = $ prev_icon. $ icon. $ v [$ field_name];
}
Unset ($ v [$ field_level], $ v [$ field_last], $ v [$ field_son_num]);
If (isset ($ v [$ field_all_son_num]) unset ($ v [$ field_all_son_num]);
$ Datas [$ k] = $ v;
}
If ($ return = 'kids '){
Return $ ids;
} Else {
Return $ datas;
}
} Else {
Return $ datas;
}
}
List Effect
Drop-down box Effect
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB