Recursive applications (2) infinitus classification and recursive applications unlimited Classification

Source: Internet
Author: User

Recursive applications (2) infinitus classification and recursive applications unlimited Classification

Implement infinitus classification using recursive Methods

Generally, when I write a project, when I write a category for a mall, for example, the upper-level category of the corresponding category is implemented, or the upper-level departments managed by other project departments generally use infinitus classification for classification.

Step 1: first, when designing a data table, if you want to implement an infinitus classification, I usually add one more field pid to the data table. Below I will explain it through a new data table,

(1) create a 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 (32) not null,

Nickname varchar (32) default null,

Pid tinyint (10) unsigned default null,

Sort mediumint (10) unsigned DEFAULT 50

) ENGINE = InnoDB default charset = utf8;

 

(2) Insert data:

------------------------------

-- Records of pid

------------------------------

Insert into 'pid 'values ('1', 'tech', '', '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', 'food', '', '0', '50 ');

Insert into 'pid 'values ('5', 'artificial intelligence ', '', '1', '50 ');

Insert into 'pid 'values ('6', 'Robot ', '', '5', '50 ');

Insert into 'pid 'values ('7', 'unmanned airway', '', '5', '50 ');

Insert into 'pid 'values ('8', 'unmanned auto', '', '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', 'hangzhou', '', '2', '50 ');

Insert into 'pid 'values ('12', 'carrier aircraft ', '', '2', '50 ');

Insert into 'pid 'values ('13', 'October ', '', '2', '50 ');

Insert into 'pid 'values ('14', 'Missile ', '', '2', '50 ');

Insert into 'pid 'values ('15', 'China' on the tip of the tongue, '', '3', '50 ');

Insert into 'pid 'values ('16', 'sichuan food', '', '15', '50 ');

Insert into 'pid 'values ('17', 'cantonese food', '', '15', '50 ');

Insert into 'pid 'values ('18', 'hunan food', '', '15', '50 ');

Insert into 'pid 'values ('19', 'bio', '', '4', '50 ');

Insert into 'pid 'values ('20', 'animal ', '', '19', '50 ');

Insert into 'pid 'values ('21', 'Plant ', '', '19', '50 ');

Insert into 'pid 'values ('26', 'haha ', 'haha', '0', '50 ');

Insert into 'pid 'values ('27', ' ',' ', '26', '50 ');

Insert into 'pid' VALUES ('28', 'boo ', 'boo', '26', '50 ');

Data Table:

 

This is probably the case for data tables.

Step 2: Enter the topic, infinitus Classification

<? Php // set the character set header ('content-type: text/html; charset = UTF-8 ');/*** infinitus Classification * @ param $ list array () * return array * // infinitus classification, which can be used to classify parent-child data. function category ($ arr, $ pid = 0, $ level = 0) {// define a static variable, store an empty array, and use a static variable because the static variable will not be destroyed and will save the previously reserved value. When the function ends, will die, the growth cycle function starts to end with the function, call again to start growing // save an empty array static $ list = array (); // query whether the class belongs to the top-level parent class through traversal. pid = 0 is the top-level parent class, and foreach ($ arr as $ value) {// determines if the pid is 0, so it is a top-level parent class and put it into the defined empty array if ($ value ['pid '] = $ pid) {// Add spaces to layer $ arr ['level'] = $ level; $ list [] = $ value; // recursive point, calls itself, use the primary key id of the top-level parent class as the parent class and then 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 an infinitus classification that I understand and summarizes some of my knowledge points. We hope you can give us some suggestions, learn together, and make progress together. Thanks ~

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.