What this article brings to you is about how PHP implements the tree-like effect of classification. (with code), there is a certain reference value, the need for friends can refer to, I hope to help you.
We sometimes need to display the categorical data in a tree-like effect, and at a glance we can see the hierarchy of organizational relationships, which are implemented as follows:
Note: I am talking about the classification for the Infinite Pole classification, as the following function refers to the table field name has three ID PID name, if there is a definition of different names please query the categorical list data when corresponding to the field alias corresponding to the ID PID name, such as your PID table field name defined as parent_id so query When the field is named "parent_id as PID";
The functions are as follows:
/** * $list for the query out of the two-dimensional array **/function gettree ($list, $pid =0, $itemprefix = ") {Static $icon = Array (' │ ', ' ├ ', ' └ '); Static $nbsp = " "; Static $arr = Array (); $number = 1; foreach ($list as $row) { if ($row [' pid '] = = $pid) { $brotherCount = 0; Determine how many siblings currently have a foreach ($list as $r) { if ($row [' pid '] = = $r [' pid ']) { $brotherCount + +; } } if ($brotherCount >0) { $j = $k = '; if ($number = = $brotherCount) { $j. = $icon [2]; $k = $itemprefix? $NBSP: '; } else{ $j. = $icon [1]; $k = $itemprefix? $icon [0]: '; } $spacer = $itemprefix? $itemprefix. $j: "; $row [' name '] = $spacer. $row [' name ']; $arr [] = $row; $number + +; Gettree ($list, $row [' id '], $itemprefix. $k. $nbsp); }}} return $arr;}
Examples of Use:
<?php header ("content-type:text/html; Charset=utf8 "), function Gettree ($list, $pid =0, $itemprefix =") {Static $icon = Array (' │ ', ' ├ ', ' └ '); Static $nbsp = " "; Static $arr = Array (); $number = 1; foreach ($list as $row) {if ($row [' pid '] = = $pid) {$brotherCount = 0; Determine how many siblings currently have a foreach ($list as $r) {if ($row [' pid '] = = $r [' pid ']) {$brotherCount + +; }} if ($brotherCount >0) {$j = $k = '; if ($number = = $brotherCount) {$j. = $icon [2]; $k = $itemprefix? $NBSP: "; }else{$j. = $icon [1]; $k = $itemprefix? $icon [0]: "; } $spacer = $itemprefix? $itemprefix. $j: "; $row [' name '] = $spacer. $row [' name ']; $arr [] = $row; $number + +; Gettree ($list, $row [' id '], $itemprefix. $k. $nbsp); }}} return $arr;} $list the data from the mock database $list = Array (), $list = [' id ' =>1, ' pid ' =>0, ' name ' = '],[' id ' =>9, ' pid ' =>7, ' Name ' + ' personnel two Group Staff 2 '],[' ID ' =>2, ' pid ' =>1, ' name ' = ' hr manager '],[' id ' =>3, ' pid ' =>2, ' name ' = ' Personnel one leader '],[' id ' =>13, ' pid ' =>12, ' Name ' = = ' technology A group of staff 1 '],[' id ' =>4, ' pid ' =>3, ' name ' + ' personnel a group of staff 1 '],[' id ' =>5, ' pid ' =>3, ' name ' = ' Personnel a group of staff 2 '],[' id ' =>6, ' pid ' =>3, ' name ' and ' + ' personnel a group of staff 3 '],[' id ' =>7, ' pid ' =>2, ' name ' = ' Personnel two leader '],[' ID ' 8, ' pid ' =>7, ' name ' + ' personnel two Group staff 1 '],[' id ' =>10, ' pid ' =>7, ' name ' = ' Personnel two Group Staff 3 '],[' id ' =>15, ' pid ' =>12, ' Name ' = = ' technology A group of staff 3 '],[' id ' =>11, ' pid ' =>1, ' name ' = ' technology manager '],[' id ' =>12, ' pid ' =>11, ' name ' = ' Technology a leader ' ],[' id ' =>14, ' pid ' =>12, ' name ' = ' Technology A group of Staff 2 '],];//execute function $list = Gettree ($list);? > <! DOCTYPE html>
Effect: