Usually when I write the project, when I write some such as the mall classification when the corresponding classification of the superior classification, or other project department management of the parent department will generally use the unlimited pole classification to classify
First step: First in the data table design, if you want to achieve the infinite pole classification, generally I will add a field in the data table PID, the following I through a new data table to illustrate,
(1) Build table:
-- ----------------------------
--Table structure for PID
-- ----------------------------
DROP TABLE IF EXISTS ' pid ';
CREATE TABLE PID (
ID tinyint unsigned not NULL auto_increment primary key comment ' primary key ID ',
Name varchar (+) is not NULL,
Nickname varchar (+) DEFAULT NULL,
PID tinyint (Ten) unsigned DEFAULT NULL,
Sort Mediumint (unsigned DEFAULT 50)
) Engine=innodb DEFAULT Charset=utf8;
(2) Insert data:
-- ----------------------------
--Records of PID
-- ----------------------------
INSERT into ' pid ' VALUES (' 1 ', ' technology ', ' ', ' 0 ', ' 50 ');
INSERT into ' pid ' VALUES (' 2 ', ' military ', ' ', ' 0 ', ' 50 ');
INSERT into ' pid ' VALUES (' 3 ', ' Man and Nature ', ' ', ' 0 ', ' 50 ');
INSERT into ' pid ' VALUES (' 4 ', ' Gourmet ', ' ', ' 0 ', ' 50 ');
INSERT into ' pid ' VALUES (' 5 ', ' ai ', ' ', ' 1 ', ' 50 ');
INSERT into ' pid ' VALUES (' 6 ', ' Robot ', ' ', ' 5 ', ' 50 ');
INSERT into ' pid ' VALUES (' 7 ', ' Unmanned aircraft ', ' ', ' 5 ', ' 50 ');
INSERT into ' pid ' VALUES (' 8 ', ' Unmanned cars ', ' ', ' 5 ', ' 50 ');
INSERT into ' pid ' VALUES (' 9 ', ' Military robot ', ' haha ', ' 6 ', ' 50 ');
INSERT into ' pid ' VALUES (' 10 ', ' Service Robot ', ' ', ' 6 ', ' 50 ');
INSERT into ' pid ' VALUES (' 11 ', ' Carrier ', ' ', ' 2 ', ' 50 ');
INSERT into ' pid ' VALUES (' 12 ', ' Carrier Machine ', ' ', ' 2 ', ' 50 ');
INSERT into ' pid ' VALUES (' 13 ', ' Warning machine ', ' ', ' 2 ', ' 50 ');
INSERT into ' pid ' VALUES (' 14 ', ' missiles ', ' ', ' 2 ', ' 50 ');
INSERT into ' pid ' VALUES (' 15 ', ' The tongue of China ', ' ', ' 3 ', ' 50 ');
INSERT into ' pid ' VALUES (' 16 ', ' Szechuan ', ' ', ' 15 ', ' 50 ');
INSERT into ' pid ' VALUES (' 17 ', ' Cantonese ', ' ', ' 15 ', ' 50 ');
INSERT into ' pid ' VALUES (' 18 ', ' Hunan ', ' ', ' 15 ', ' 50 ');
INSERT into ' pid ' VALUES (' 19 ', ' creature ', ' ', ' 4 ', ' 50 ');
INSERT into ' pid ' VALUES (' 20 ', ' animals ', ' ', ' 19 ', ' 50 ');
INSERT into ' pid ' VALUES (' 21 ', ' Plants ', ' ', ' 19 ', ' 50 ');
INSERT into ' pid ' VALUES (' 26 ', ' haha ', ' haha ', ' 0 ', ' 50 ');
INSERT into ' pid ' VALUES (' 27 ', ' hehe hey ', ' hehe hey ', ' 26 ', ' 50 ');
INSERT into ' pid ' VALUES (' 28 ', ' Hush Hush ', ' Hush Hush ', ' 26 ', ' 50 ');
Data sheet:
This is probably the case with the data sheet.
Step two: Get to the point, unlimited pole classification
<?php//sets the character set header (' Content-type:text/html;charset=utf-8 ');/** * Infinite Pole classification * @param $list Array () * Return array *// /infinite Pole Classification, implementing a data classification function category with parent-child relationships ($arr, $pid =0, $level =0) { //define a static variable, store an empty array, use a static variable, because the static variable is not destroyed, Will save the previously reserved values, normal variables at the end of the function will die, the growth cycle function begins to the end of the function, again call restart growth //Save an empty array static $list =array (); By traversing whether the lookup belongs to the top-level parent class, Pid=0 is the top-level parent class, and foreach ($arr as $value) { //is judged if pid=0, then for the top-level parent class, put in the defined empty array if ($value [' PID ']== $pid) { //Add spaces for layering $arr [' Level ']= $level; $list []= $value; Recursive point, call itself, the primary key ID of the top-level parent class as the parent class to re-call the loop, Space +1 category ($arr, $value [' id '], $level + 1); } } return $list;//Recursive Exit}
Connection data:
Effect:
2. A small extension:
This is just my own understanding of the infinite pole classification, for their own knowledge points of some summary. There are inappropriate places we hope that everyone to give some advice, common learning, common progress. thanks~
Realization of infinite pole classification by recursive method