PHP implements unlimited classification, and php implements unlimited classification.

Source: Internet
Author: User

PHP implements unlimited classification, and php implements unlimited classification.

Unlimited classification is a design technique that is often used during development, such as website directory, Department structure, and document classification. I think it plays a major role in designing the hierarchical structure of tables. For example, on some platforms,

It is a relationship between superiors and subordinates. The superiors have multiple subordinates, and the subordinates have their own branches. Most of them are implemented using recursive ideas. Let's not talk much about it. First, let's take a look at the implementation of recursion.

Recursion (Program Calling skills ):

1. $ _ GLOBALS [result]

2. static $ result

3. Parameter Reference &

Example: traverse 1-10

```$i=0;function deeploop( $i ){  global $i;  $i++;  echo $i;  if( $i<10 ){    deeploop($i);  }}function deeploop( ){  static $i=0;  $i++;  echo $i;  if( $i<10 ){    deeploop($i);  }}function deeploop( &$i=0 ){  $i++;  echo $i;  if( $i<10 ){    deeploop($i);  }}```

I. Unlimited classification implementation:

1. Table Design settings parent id top-level parent id is set to 0 to create a genealogy tree; each category needs to record its parent id. (Pid = 0 indicates top level)

Id pid catename cateorder createtime (primary key id, parent id, category name, category sorting, Creation Time)

Example: Classification directory structure of a website, classification structure of catering, and comment Structure

2. Full path unlimited classification (record all parent IDs in one field in order)

Id path catename cateorder createtime (primary key id, in the order of comma-separated parent id, category name, category sorting, Creation Time)

Advantages and disadvantages:

Convenient full-path query; added; Data maintenance during mobile classification is slightly complicated;

Ii. Example implementation (website directory ):

Category Table:

''' # Create table 'destpcate' ('id' int (11) not null AUTO_INCREMENT primary key, 'pid 'int (11) not null default 0, 'catename' char (25) not null, 'cateorder' int (6), 'createtime' date) ENGINE = INNODB default CHARSET = utf8; // INSERT data INTO 'destpcate' VALUES (2016, 'img ', null, '2017-11-01'), (, 'female ', null, '2017-11-01 '), (2016, 1, 'News', null, '2017-11-01'), (2016, 2, 'soccer baby', null, '2017-11-01 '), (5, 2, 'Japan and South Korea start', null, '2017-11-01 '), (6, 5, 'beauty photo', null, '2017-11-01 '); # Full path create table 'qljcate' ('id' int (11) not null, 'path' char (255), 'catename' char (25) not null, 'cateorder' int (6), 'createtime' date) ENGINE = INNODB default CHARSET = utf8; insert into 'qljcate' VALUES (1, null, 'image', null, '2017-11-01 '), (2016, 'beauty image', null, '2017-11-01'), (2016, 2, 'soccer baby', null, '2017-11-01 '), (2016-11-01', 2, 'Japan and South Korea start', null, '2017-11-01 '), (2016, 'beauty photo', null, '2017-11-01 ');'''

Obtain the directory path of the image:

''' # Create table 'destpcate' ('id' int (11) not null AUTO_INCREMENT primary key, 'pid 'int (11) not null default 0, 'catename' char (25) not null, 'cateorder' int (6), 'createtime' date) ENGINE = INNODB default CHARSET = utf8; // INSERT data INTO 'destpcate' VALUES (2016, 'img ', null, '2017-11-01'), (, 'female ', null, '2017-11-01 '), (2016, 1, 'News', null, '2017-11-01'), (2016, 2, 'soccer baby', null, '2017-11-01 '), (5, 2, 'Japan and South Korea start', null, '2017-11-01 '), (6, 5, 'beauty photo', null, '2017-11-01 '); # Full path create table 'qljcate' ('id' int (11) not null, 'path' char (255), 'catename' char (25) not null, 'cateorder' int (6), 'createtime' date) ENGINE = INNODB default CHARSET = utf8; insert into 'qljcate' VALUES (1, null, 'image', null, '2017-11-01 '), (2016, 'beauty image', null, '2017-11-01'), (2016, 2, 'soccer baby', null, '2017-11-01 '), (2016-11-01', 2, 'Japan and South Korea start', null, '2017-11-01 '), (2016, 'beauty photo', null, '2017-11-01 ');'''

* Note:

A mobile category cannot be moved to itself or its sub-classes. You can only delete the lowest-level category and non-subclass category. In other words, you can only delete the category from the bottom layer ).

Think about all the image categories under the image type?

''' # Pid parent-level id implementation function GetAllcate ($ id, & $ result = array ()) {$ SQL = "SELECT * FROM deepcate WHERE pid in ({$ id})"; $ query = mysql_query ($ SQL); $ row = mysql_fetch_assoc ($ query ); if (mysql_num_rows ($ row)> 0) {$ idlist = array (); while ($ row) {$ result [] = $ row; $ idlist [] = $ row ['id'];} $ id = implode (',', $ idlist); GetAllcate ($ id, $ result );} $ result = array_unique ($ result); return $ result ;}'''

This method is suitable for querying all books and articles under the parent category... Of course, the full path can be obtained directly, so it is not mentioned here.

In practice, we can design a reasonable table structure based on the actual situation.

For example, if a chain store manages its commodities, its account settings will have a parent-subordinate relationship. Such a subordinate may have its own subordinates according to the partition of the agent site, using this infinite classification method is more flexible in the face of this relationship system. You only need to set each account level to assign permissions to different stores.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.