1, do the general site will encounter the problem of processing classification, I have to post several examples of processing unlimited classification
The structure of the database is simple:
ID, fatcher_id, name, ...
This design is short and concise, fully meet the 3NF. Can be completely the majority of requirements, OK, let's look at this database structure under the implementation of the program.
1. Recursive query Database
The most deadly way to do it is also the best way 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 query the database once, in any area where you are involved in the classification (page, query ...), the database will be a tragic operation of the hard disk .... Amen, ~so~ jump.
2. Query the database once, recursive array to generate the above directory structure
Posts a general idea
Function SelectList (& $Data, $RootID = 0 ')
{
for ($i = 0; $i < count ($Data); $i + +)
{
if ($Data [$i] [' UID '] = = $RootID)
{
...//handle, generate HTML directly or save in array ok
$this->selectrecursion ($Data, $Data [$i] [' ID '], $blank, $Match);
}
}
return $this->output;
}
This strength of the recursive general site is enough ~ but encountered BT point, there are a few k or even on the W classification, recursion may take more than hundred milliseconds, in the case of concurrency .... Oh ~ Let's dance a little bit more.
3. Querying the database once, non-recursive generation of the directory structure
This step, the skill of the program came ~ only to the result of a traversal once can generate the above directory structure, want to format it into a Web page display style is very convenient ~ ~ The following is written by others, I tried to 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 cluster parent ID pointed to by the previous node
for ($cnt = Count ($Data), $i < $cnt;)//calendar entire categorical array
{
$j = 0; Initialize the classification data count
if ($UpID = = $RootID)//Save all primary classifications to $output in the first loop
{
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 in this array
$tmp [$Data [$i] [' ID ']] = & $Output [$j]; and save the Node ID in the output location.
$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 looked very tiring AH ~ This code is more efficient than the previous paragraph more than 10 times times faster, the strength is big enough ...
But.... 90% of the site with such code is wasted ~ I have encountered the classification of the site is generally below the level of four, then, can you optimize it again?........ Again .... Let's jump a little bit.
4. Start with the database ~
Slightly change the structure of the database, plus a layer of redundant fields, this is a DB desing master thought out, I put him out just
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
Now search by the size of the layer: SELECT * from type_table_2 ORDER by Type_layer
List the recordset 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
See ~ A query even the directory structure is generated well, so that the program is much easier, just a lot of maintenance layer field work, the layer field in this routine can handle 99 categories, if there is a BT application, change a little more on the line, hehe, analyze your needs first
Ok~over~-The flash of a man ~
The above describes the inability to access you may not have permission to use network resources post a few PHP unlimited classification implementation Ideas ~, including the inability to access the network resources you may not have permission to use the content, I hope to be interested in PHP tutorial friends helpful.