Post a few PHP unlimited categories to achieve ideas ~_php tips

Source: Internet
Author: User
1,Do the site generally encounter the problem of dealing with classification, I have to post several examples of processing infinite classification
The structure of the database is simple:
ID, fatcher_id, name, ...
This design is short and short, fully meet the 3NF. Can be completely overwhelming, OK, let's look at this database structure of the implementation of the program.

1. Recursive query Database
The worst way to do it is to do it best.
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 query the database once, in any of your categories involved (page, query ...), the database will be the tragic operation of the hard drive .... Amen, ~so~, jump.
2. Query the database once, recursive array 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)
{
...//processing, generating HTML directly or saving into arrays OK
$this->selectrecursion ($Data, $Data [$i] [' ID '], $blank, $Match);
}
}
return $this->output;
}
This strength of the recursive general Web site is enough ~ but encountered BT point, there are a few k even on the classification of W, recursion may be more than hundred milliseconds, in the case of concurrent ... Oh ~ Let's dance again
3. Querying a database, not recursively generating directory structure
This step, the program's skills come ~ Only the results of the traversal can be generated above the directory structure, want to format it as a Web page display style is very convenient slightly ~ below the department of others to write, I tried completely feasible
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; The category parent ID that the previous node points to
for ($cnt = Count ($Data); $i < $cnt;)//Calendar The entire array of categories
{
$j = 0; Initialize the categorized data count
if ($UpID = = $RootID)//////Save all first-level categories to $output this array
{
while ($Data [$i] [' UID '] = = $UpID && $i < $len)//Determine if the previous node is a sibling node
{
$Output [$j] = $Data [$i]; Save the node to output this array
$tmp [$Data [$i] [' ID ']] = & $Output [$j]; and 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 output
}
$i + +;
$j + +;
}
}
$UpID = $Data [$i] [' UID '];
}
return $Output;
}

The program looks so tiring AH ~ This code is more efficient than the previous paragraph more than 10 times times, the strength of the system is big enough ...
But.... 90% of the site with such a code is wasted ~ I have encountered the site classification is generally below four, then, can be optimized again?........ Again .... Let's jump again.
4. Start with the database ~
Slightly change the structure of the database, add a layer redundant field, this is the DB desing master thought out, I put him out
ID Name father_id Layer
1 Total Categories 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

Now search by layer size: SELECT * from type_table_2 ORDER by Type_layer

The recordset is listed as follows:

ID Name father_id Layer
1 Total Categories 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

Did you see it? A query directory structure is generated well, this program is a lot easier, just more maintenance layer field work, this routine layer field each level can handle 99 categories, if there is BT application, change larger on the line, OH, first analysis of your needs

ok~over~.

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.