Example of how PHP implements multi-level classification Spanning Tree, php example

Source: Internet
Author: User
Tags php example

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.

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.