Example of how PHP implements multi-level classification Spanning Tree, php example
This example describes how PHP implements a multi-level classification spanning tree. We will share this with you for your reference. The details are as follows:
Condition. Classification in the database is performed by id and fid (parent ID!
Usage:
$ SQL = "XXXXXXXXXX"; // SQL statement $ res = $ db-> Select ($ SQL); // Execute SQL $ list = array (); treeList (treeGet ($ res), $ list);/generate the tree print_r ($ res); // print it out and have a look!
Result:
We recommend that you make the most of the news.
The Code is as follows:
/***** Select the number of SQL statements ** @ access public * @ param Array $ field information, supports Han Number * @ param Array $ table database table * @ param Array $ where condition * @ return SQL statement */function treeGet ($ data) {$ tmptree = null; $ tree = $ data; return treeAddNodeToTree ($ tmptree, treegetbyuid ($ tree, 0, @ $ field), $ tree );} /***** insert SQL Han number ** @ access public * @ param Array $ fieldResult field information, supports Han Number * @ param Array $ table database table * @ return SQL statement */function treeAddNodeToTree ($ Node, $ MiniTree, & $ source) {if (is_array ($ miniTree) {foreach ($ miniTree as $ k =>$ v) {if (! Count ($ miniTree [$ k] ['child '] = treeAddNodeToTree ($ miniTree [$ k], treegetbyuid ($ source, @ $ v ['id']), $ source) {unset ($ miniTree [$ k] ['child ']); $ miniTree [$ k] ['leaf'] = true; // set leaf Node }}return $ Node ['child '] = $ miniTree;} function treegetbyuid (& $ stree, $ uid) {$ dtree = array (); if (is_array ($ stree) {foreach ($ stree as $ k => $ v) {if ($ v ['fid'] = $ uid) {$ mytmp = array (); $ mytmp = $ v; unset ($ stree [$ k]); array_push ($ dtree, $ Mytmp); $ mytmp = null ;}} return $ dtree;}/*** update SQL Han quantity ** @ access public * @ param Array $ fieldResult field information, supports Han Number * @ param Array $ table database table * @ param Array $ where condition * @ return SQL statement */function treeMakeDeep ($ deep) {$ returnValue = ""; for (; $ deep --) {$ returnValue. = "success";} return $ returnValue. "exist";} function treeList ($ treeData, & $ List) {static $ deep = 0; if (is_array ($ treeData )) {foreach ($ treeData as $ k => $ V) {$ v ['demovalue'] = treeMakeDeep ($ deep); $ v ['deep '] = $ deep; $ t = $ v; unset ($ t ['child ']); array_push ($ List, $ t); if ($ v ['child']) {++ $ deep; $ optionsNode. = treeList ($ v ['child '], $ List); $ deep -- ;}} if ($ lastV = array_pop ($ List )) {$ lastV ['demovalue'] = str_replace ('release', 'release', $ lastV ['demovalue']); array_push ($ List, $ lastV );}}} function treeSelect ($ tree, $ id, $ options = "child") {switch (strtolower ($ options) {case "c Hild ": $ tmpTree = array (); $ deep =-1; foreach ($ tree as $ k => $ v) {if ($ id = $ v ['id']) {array_push ($ tmpTree, $ v); $ deep = $ v ['deep '];} elseif ($ deep! =-1) {if ($ v ['deep '] <= $ deep) {break;} else {array_push ($ tmpTree, $ v) ;}} break; case "remove": default: $ tmpTree = array (); $ deep =-1; foreach ($ tree as $ k => $ v) {if ($ id = $ v ['id']) {$ deep = $ v ['deep ']; continue;} elseif ($ deep! =-1) {if ($ v ['deep '] <= $ deep) {array_push ($ tmpTree, $ v); $ deep =-1 ;}continue ;} array_push ($ tmpTree, $ v) ;}return $ tmpTree ;}
PS:To help readers read the source code, the above Code uses the online tool http://tools.jb51.net/code/jb51_php_formatfor formatting.