This article mainly introduces the method of implementing multilevel classification spanning tree in PHP, involving Php+mysql database operations and array and string traversal, substitution, combination and other related operation skills, the need for friends can refer to the following
Specific as follows:
Conditions, the classification in the database is based on ID,FID (parent ID) to achieve multilevel classification!
How to use:
$sql = "Xxxxxxxxxx"; SQL statement $res = $db->select ($sql); Execution Sql$list=array (); Treelist (Treeget ($res), $list); /Spanning Tree print_r ($res); Print it out and look!
Usage Result:
┣ Recommended News AH ┃┣ International news ┃┣dfffffg┃┣ttttttt┃┃┗yyyyy
The code is as follows:
/*** Select the number of SQL * * * @access public* @param array $field field information, support for the number of * @param array $table database table * @param array $where condition * @ret URN SQL SQL statement */function treeget ($data) {$tmptree =null; $tree = $data; Return Treeaddnodetotree ($tmptree, Treegetbyuid ($tree, 0,@ $field), $tree);} /*** Insert SQL * * @access public* @param array $fieldResult field information, support for the number of columns * @param array $table database table * @return SQL SQL statements */functi On 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 * * * @access public* @param array $fieldResult field information, support for Culvert * @param array $table database table * @param array $where condition * @return SQL SQL statement */function treemakedeep ($deep) {$returnValue = ""; for (; $deep; $deep-) {$returnValue. = "┃"; } return $returnValue. " ┣ ";} function treelist ($treeData,& $List) {static $deep = 0; if (Is_array ($treeData)) {foreach ($treeData as $k = = $v) {$v [' Deepvalue ']=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 [' Deepvalue ']=str_replace (' ┣ ', ' ┗ ', $lastV [' Deepvalue ']); Array_push ($List, $lastV); }}}function Treeselect ($tree, $id, $options = "Child") {switch (Strtolower ($options)) {case ' child ': $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;}
The above is the whole content of this article, I hope that everyone's study has helped.