I believe many of the small partners learn PHP will try to do an online mall as a way to improve their technology. A variety of commodity classification, commodity name, such as the operation should be handy, then you can try to the unlimited level of the production of classified lists.
To a search on the internet PHP Unlimited polar classification, a lot of, but a lot of is a, and, write a very messy, code a lot, let us how to learn, those are not reliable, or their own ramming drum infinite pole classification.
What is an infinite class classification?
The infinite level classification is a kind of classification technique, such as Department organization, Article Classification, Discipline classification and so on commonly to the infinite level classification, it is easy to understand it as a classification. In fact, we think carefully, the classification of life is simply too much, clothes can be divided into men's and women's clothing, can also be divided into tops and trousers, can also be classified according to age. Classification is everywhere, the classification appears "infinite". I'm not talking about the need for infinite classification here.
Introduction to the principle of infinite class classification
Infinite classification seems to be "tall", in fact, the principle is very simple. The infinite classification not only needs the ingenuity of the code, but also relies on the rationality of the database design. To satisfy an infinite level of classification, the database needs to have two required fields, Id,pid. ID is used to identify itself, and PID is used to indicate the parent ID. That is, each classification record not only describes itself, but also describes another ID that is most closely associated with it. Seemingly complex things are solved by such a small trick.
Gossip is not much to say, it is time to show examples of this article.
As an avid sea thief fan, this example I will be "Pirates of the King" character organization to do the case.
Database preparation:
Build Table OnePiece:
CREATE TABLE onepiece (
ID int auto_increment,
pid int not NULL,
name varchar (?) not NULL,
primary KEY (i d)
);
To insert test data:
Insert OnePiece values
(1, 0, ' Navy '),
(2,0, ' sea Thief '), (3,0, ' Revolutionary Army '), (
4,1, ' Green Pheasant '), (
5,1, ' Red Dog
'), (6,1 , ' The Yellow Ape '), (
7,2, ' The Four Kings '), (
8,2, ' Seven Wu Hai '), (
9,2, ' The straw Hat, The Pirate Regiment '), (
10,9, ' Sauron '
), (11,7, ' The Fox '), (12,8, ' Doverland '),
(13,8, ' Klockdal ');
This is also the popular science of the King inside the setting: The world is divided into three camps: Navy, sea thieves, the revolutionary Army. Navy has generals: Green Pheasant, Red Dog, Yellow Ape. Sea thieves have: Four emperor, seven Wu Hai, Straw Hat Sea Thief Regiment. Four emperors have the flamenco, seven Wu Sea has many elder brother, Klockdal, straw hat Sea Thief Regiment has Sauron. (Advertising: The Sea thief did very good-looking).
Final Purpose:
We are making today two forms of infinite-level classification, one is Drop-down list, one is navigation link type. Directly on the effect diagram:
drop-down list
Navigation link Type
Instance code:
I encapsulated a unlimited class that called diaplaylist () to display the Drop-down list form and Invoke Diaplaylink to show the navigation link classification. You can also add (AddNodes ()) and delete (Deletenodes) categories.
<?php class unlimited{protected $mysqli;
Public function __construct ($config) {$this->mysqli=new mysqli ($config [' Host '], $config [' User '], $config [' pwd ']);
$this->mysqli->select_db ($config [' db ']);
$this->mysqli->set_charset (' UTF8 ');
if ($this->mysqli->connect_errno) {echo $this->mysqli->connect_error;
The Private function getlist ($pid =0,& $result =array (), $spac =0) {$spac = $spac +2;
$sql = "SELECT * from OnePiece where pid={$pid}";
$rs = $this->mysqli->query ($sql);
while ($row = $rs->fetch_assoc ()) {$row [' name ']=str_repeat (' ', $spac). $row [' name '];
$result []= $row;
$this->getlist ($row [' id '], $result, $SPAC);
return $result;
/** * Show Drop-down list type * @return [type]/Public function displaylist () {$rs = $this->getlist ();
$str = "<select name= ' cate ' >"; foreach ($rs as $key => $val) {$str. = "<option >{$val [' Name ']}</option> ";
$str. = "</select>";
return $str;
Private Function GetLink ($cid,& $result =array ()) {$sql = "select * from OnePiece where id={$cid}";
$rs = $this->mysqli->query ($sql);
if ($row = $rs->fetch_assoc ()) {$result []= $row;
$this->getlink ($row [' pid '], $result);
Return Array_reverse ($result); /** * Show Navigation link * @param [type] $cid [description] * @return [type] [description]/Public function di
Splaylink ($cid) {$rs = $this->getlink ($cid);
$str = ';
foreach ($rs as $val) {$str. = "<a href=" >{$val [' name ']}</a>>];
return $str;
/** * Add Category * @param [Type] $PID parent class ID * @param [type] $name this class name/Public function addnodes ($pid, $name) {
$sql = "INSERT into onepiece values (', {$pid}, '". $name.) ";
if ($this->mysqli->query ($sql)) {return true;
}/** * Delete category * @param [Type] $id this class ID * @return [type] * *Public Function Deletenodes ($id) {$sql = ' select * from onepiece where PID ={$id} ';
$rs = $this->mysqli->query ($sql);
if ($row = $rs->fetch_assoc ()) {$mes = "and child elements, do not delete";
}else{$sql = "Delete from onepiece where id={$id}";
if ($this->mysqli->query ($sql)) {$mes = "Delete succeeded";
} return $mes;
}
}
The function of the class mainly adopts the recursive function method, and if the understanding of recursive function is understood deeply, the rest of the part will be ripe. I'll explain in detail the three ways to implement recursive functions in the following sections.
Let's look at one more example:
First set up the classification information table:
CREATE TABLE IF not EXISTS ' category ' (
' CategoryID ' smallint (5) unsigned not NULL auto_increment,
' ParentID ' smal Lint (5) unsigned not null DEFAULT ' 0 ',
' categoryname ' varchar (+) NOT NULL,
PRIMARY KEY (' CategoryID ')
Insert several data:
INSERT into ' category ' (' CategoryID ', ' ParentID ', ' CategoryName ') VALUES
(1, 0, ' php '),
(2, 0, ' Java '),
(3, 0 , ' C + + '),
(4, 1, ' PHP base '),
(5, 1, ' php open-source material '),
(6, 1, ' PHP Framework '), (7, 2, '
java Se '),
(8, 2, ' Java EE '),
(9, 2, ' Java Me '),
(3, ' C + + BASIC programming '),
(one, 3, ' C/C + + system development '), (+,
3, ' C. Embedded programming '),
(13, 3, ' C + + application development ', (d,
' C + + desktop application development '), (+,
' C + + game development ');
Here is the PHP code:
<?php//php Infinite Polar Classification//Get a direct subcategory of a classification function getsons ($categorys, $catId =0) {$sons =array ();
foreach ($categorys as $item) {if ($item [' ParentID ']== $catId) $sons []= $item;
return $sons;
}//Get all subcategory function Getsubs ($categorys, $catId =0, $level =1) {$subs =array () of a taxonomy;
foreach ($categorys as $item) {if ($item [' ParentID ']== $catId) {$item [' level ']=];
$subs []= $item;
$subs =array_merge ($subs, Getsubs ($categorys, $item [' CategoryID '], $level + 1));
} return $subs;
//Get all Parent Classification/method One of a category, recursive function getparents ($categorys, $catId) {$tree =array (); foreach ($categorys as $item) {if ($item [' CategoryID ']== $catId) {if ($item [' ParentID ']>0) $tree =array
_merge ($tree, Getparents ($categorys, $item [' ParentID ']));
$tree []= $item;
Break
} return $tree;
}//Method Two, iterative function getParents2 ($categorys, $catId) {$tree =array (); while ($catId!= 0) {foreach ($categorys as $item) {if ($item [' CategoryID ']== $catId) {$tree []=];
$catId = $item [' ParentID '];
Break
}} return $tree;
//test Section $pdo =new PDO (' Mysql:host=localhost;dbname=test ', ' root ', ' 8888 ');
$stmt = $pdo->query ("SELECT * from Category Order by CategoryID");
$categorys = $stmt->fetchall (PDO::FETCH_ASSOC);
$result =getsons ($categorys, 1); foreach ($result as $item) echo $item [' CategoryName ']. '
<br> ';
Echo '
Look at the final result.
Although this article introduces the use of recursive implementation of the infinite level of classification, but in fact, do not recommend that you do, we know that the classification of many, recursive efficiency is also low, this article here is only to let everyone better understand recursion to do so.