Post several PHP unlimited classification implementation ideas ~ _ PHP Tutorial

Source: Internet
Author: User
Post several PHP unlimited classification implementation ideas ~. 1. websites generally encounter classification processing problems. The structure of the database is very simple: id, fatcher_id, name ,...... this design is short and concise 1,Websites generally encounter classification problems. I will occasionally post several examples to process unlimited classification.
The database structure is simple:
Id, fatcher_id, name ,......
This design is short and concise, fully satisfying 3NF... it can satisfy the vast majority of requirements. OK, let us look at the program implementation under this database structure.

1. recursive database query
The most terrible practice ~ It is also the best practice to achieve
Category 1
Category 1.1
Category 1.1.1
Category 1.2
Category 2
Category 2.1
Category 3
Category 3.1
Category 3.2
......
In order to generate such a directory structure, the program recursively queries the database once, where you are involved in any category (paging, querying ....), the database will be operating the hard disk sadly .... amen ~ So ~ Skip it...
2. query the database once and use recursive arrays to generate the above directory structure
Post a general idea
Function SelectList (& $ Data, $ RootID = 0 ')
{
For ($ I = 0; $ I <count ($ Data); $ I ++)
{
If ($ Data [$ I] ['uid'] = $ RootID)
{
... // Process, generate HTML directly or save it to the array.
$ This-> SelectRecursion ($ Data, $ Data [$ I] ['id'], $ blank, $ Match );
}
}
Return $ this-> Output;
}
This kind of strength recursion is generally enough for websites ~ However, if we encounter a BT point, there may be several K or even a W classification, it may take more than a hundred milliseconds to hand it over again. when considering concurrency, we will ~ Let's try again.
3. query the database once to generate a non-recursive directory structure
In this step, the program skills are coming ~ The directory structure above can be generated only once the result is traversed. it is very convenient to format it into the display style of the webpage ~ The following is written by other people. it is feasible to try it.
Function GetArray ($ RootID = 0)
{
$ Data = array ();

$ Data = $ tblObj-> MapResult ($ tblObj-> Select ());

$ Output = Array ();
$ I = 0;
$ Len = Count ($ Data );
If ($ RootID)
{
While ($ Data [$ I] ['uid']! = $ RootID & $ I <$ len) $ I ++;
}
$ UpID = $ RootID; // category parent ID pointed to by the previous node
For ($ cnt = Count ($ Data); $ I <$ cnt;) // calendar the entire category array
{
$ J = 0; // initialize the subcategory data count for this category
If ($ UpID = $ RootID) // save all Level 1 classes to the $ Output array during the first loop.
{
While ($ Data [$ I] ['uid'] = $ UpID & $ I <$ len) // checks whether the previous node is a sibling node.
{
$ Output [$ j] = $ Data [$ I]; // Save the node to the Output array.
$ Tmp [$ Data [$ I] ['id'] = & $ Output [$ j]; // Save the node ID in the Output position.
$ I ++;
$ J ++;
}
}
Else
{
While ($ Data [$ I] ['uid'] ==$ UpID & $ I <$ len)
{
If ($ tmp [$ UpID])
{
$ Tmp [$ UpID] ['child '] [$ j] = $ Data [$ I];
$ Tmp [$ Data [$ I] ['id'] = & $ tmp [$ UpID] ['child '] [$ j]; // Save the location of the node ID in the Output
}
$ I ++;
$ J ++;
}
}
$ UpID = $ Data [$ I] ['uid'];
}
Return $ Output;
}

The program looks so tired ~ The efficiency of this code is more than a dozen times faster than that of the previous one, and the intensity is large enough...
However, 90% of websites are wasting time using such code ~ Websites that have been encountered are generally classified at or below level 4. can you optimize them ?........ ... Jump again ..
4. start with the database ~
Slightly modify the database structure and add a layer redundancy field. this is what db desing experts have come up with. I just post it.
Id name father_id layer
1. total Category 0 000000
2 Category 1 1 010000
3 Category 1.1 2 010100
4 Category 1.2 2 010200
5 Category 2 1 020000
6 Category 2.1 5 020100
7 Category 3 1 030000
8 Category 3.1 7 030100
9 Category 3.2 7 030200
10 Category 1.1.1 3 010101

Search BY layer size: SELECT * FROM Type_table_2 order by type_layer

The record set is listed as follows:

Id name father_id layer
1. total Category 0 000000
2 Category 1 1 010000
3 Category 1.1 2 010100
10 Category 1.1.1 3 010101
4 Category 1.2 2 010200
5 Category 2 1 020000
6 Category 2.1 5 020100
7 Category 3 1 030000
8 Category 3.1 7 030100
9 Category 3.2 7 030200

Have you seen it ~ The directory structure is generated after one query ~, In this way, the program is much easier, but it only requires maintenance of layer fields. in this routine, layer fields can process 99 categories at each level. if there is a BT application, you just need to increase it.

OK ~ OVER ~ Transient person ~

Websites generally encounter classification processing problems. The structure of the database is very simple: id, fatcher_id, name ,...... this design is short and concise...

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.