In-depth analysis of PHP unlimited classification (tree type) _ PHP Tutorial

Source: Internet
Author: User
Tags smarty template
In-depth analysis of PHP unlimited classification (tree type. PHP unlimited classification, Google will find a lot of relevant information, the idea is more popular, but also a lot of use is that the classification table has at least three fields: id, pid, name, id auto-incrementing table classification, PHP unlimited classification, Google will find a lot of relevant information, the idea is more popular, it is also used more, that is, the classification table has at least id, pid, the name field consists of three fields: id auto-incrementing table category, pid is the parent category, and name is the category name. in this way, a tree is formed, as shown in the following figure.

The code is as follows:


// Simulate the PHP unlimited Classification Query Results
Return array (
Array (
'Id' => 1,
'Pid '=> 0,
'Name' => 'homepage'
),
Array (
'Id' => 2,
'Pid '=> 0,
'Name' => 'news'
),
Array (
'Id' => 3,
'Pid '=> 0,
'Name' => 'media'
),
Array (
'Id' => 4,
'Pid '=> 0,
'Name' => 'download'
),
Array (
'Id' => 5,
'Pid '=> 0,
'Name' => 'About ours'
),
Array (
'Id' => 6,
'Pid '=> 2,
'Name' => 'tianchao News'
),
Array (
'Id' => 7,
'Pid '=> 2,
'Name' => 'overseas News'
),
Array (
'Id' => 8,
'Pid '=> 6,
'Name' => 'state Official News'
),
Array (
'Id' => 9,
'Pid '=> 3,
'Name' => 'music'
),
Array (
'Id' => 10,
'Pid '=> 3,
'Name' => 'Cine'
),
Array (
'Id' => 11,
'Pid '=> 3,
'Name' => 'Fiction'
),
Array (
'Id' => 12,
'Pid '=> 9,
'Name' => 'ringbe'
),
Array (
'Id' => 13,
'Pid '=> 9,
'Name' => 'pop music'
),
Array (
'Id' => 14,
'Pid '=> 9,
'Name' => 'classical music'
),
Array (
'Id' => 15,
'Pid '=> 12,
'Name' => 'Popular ringtones'
),
Array (
'Id' => 16,
'Pid '=> 12,
'Name' => 'funny ringtones'
),
Array (
'Id' => 17,
'Pid '=> 12,
'Name' => 'MP3 ringtones'
),
Array (
'Id' => 18,
'Pid '=> 17,
'Name' => '128k'
),
Array (
'Id' => 19,
'Pid '=> 8,
'Name' => 'Entertainment News'
),
Array (
'Id' => 20,
'Pid '=> 11,
'Name' => 'traversal class'
),
Array (
'Id' => 21,
'Pid '=> 11,
'Name' => 'Martial Arts class'
),
);
?>


The category-related operations provided by those articles are somewhat frustrated, and database operations are directly encapsulated. That is, if someone else wants to use your class and create the same table as you, it's really disgusting. Because the project needs to be used, I wrote a PHP unlimited classification class (also called the Tree class) without database operations. I only need to import the result set when instantiating it, that is, a tree array. Then execute the leaf method or navi method to get the desired result. see the source code below. after reading it, we will provide the corresponding template recursion method of the smarty template engine.

The code is as follows:


/**
* Tree type (unlimited classification)
*
* @ Author Kvoid
* @ Copyright http://kvoid.com
* @ Version 1.0
* @ Access public
* @ Example
* $ Tree = new Tree ($ result );
* $ Arr = $ tree-> leaf (0 );
* $ Nav = $ tree-> navi (15 );
*/
Class Tree {
Private $ result;
Private $ tmp;
Private $ arr;
Private $ already = array ();
/**
* Constructor
*
* @ Param array $ result tree data table result set
* @ Param array $ fields tree data table field, array (Category id, parent id)
* @ Param integer $ Parent id of the root top-level category
*/
Public function _ construct ($ result, $ fields = array ('id', 'pid '), $ root = 0 ){
$ This-> result = $ result;
$ This-> fields = $ fields;
$ This-> root = $ root;
$ This-> handler ();
}
/**
* Tree data table result set processing
*/
Private function handler (){
Foreach ($ this-> result as $ node ){
$ Tmp [$ node [$ this-> fields [1] [] = $ node;
}
Krsort ($ tmp );
For ($ I = count ($ tmp); $ I> 0; $ I --){
Foreach ($ tmp as $ k => $ v ){
If (! In_array ($ k, $ this-> already )){
If (! $ This-> tmp ){
$ This-> tmp = array ($ k, $ v );
$ This-> already [] = $ k;
Continue;
} Else {
Foreach ($ v as $ key => $ value ){
If ($ value [$ this-> fields [0] ==$ this-> tmp [0]) {
$ Tmp [$ k] [$ key] ['child '] = $ this-> tmp [1];
$ This-> tmp = array ($ k, $ tmp [$ k]);
}
}
}
}
}
$ This-> tmp = null;
}
$ This-> tmp = $ tmp;
}
/**
* Reverse recursion
*/
Private function recur_n ($ arr, $ id ){
Foreach ($ arr as $ v ){
If ($ v [$ this-> fields [0] ==$ id ){
$ This-> arr [] = $ v;
If ($ v [$ this-> fields [1]! = $ This-> root) $ this-> recur_n ($ arr, $ v [$ this-> fields [1]);
}
}
}
/**
* Forward recursion
*/
Private function recur_p ($ arr ){
Foreach ($ arr as $ v ){
$ This-> arr [] = $ v [$ this-> fields [0];
If ($ v ['child ']) $ this-> recur_p ($ v ['child']);
}
}
/**
* Multi-dimensional menu array
*
* @ Param integer $ id Category id
* @ Return array returns the branch. by default, the entire tree is returned.
*/
Public function leaf ($ id = null ){
$ Id = ($ id = null )? $ This-> root: $ id;
Return $ this-> tmp [$ id];
}
/**
* Navigation one-dimensional array
*
* @ Param integer $ id Category id
* @ Return array returns a single-line category until the top-level category
*/
Public function navi ($ id ){
$ This-> arr = null;
$ This-> recur_n ($ this-> result, $ id );
Krsort ($ this-> arr );
Return $ this-> arr;
}
/**
* Scattered one-dimensional array
*
* @ Param integer $ id Category id
* @ Return array returns all category IDs under leaf.
*/
Public function leafid ($ id ){
$ This-> arr = null;
$ This-> arr [] = $ id;
$ This-> recur_p ($ this-> leaf ($ id ));
Return $ this-> arr;
}
}
?>


Usage of PHP unlimited classification in smarty:
$ Result = $ db-> query (......); // Query the result set here. Note that the result set is an array.
$ Tree = new Tree ($ result );
$ Arr = $ tree-> leaf (0 );
$ Nav = $ tree-> navi (15 );
$ Smarty-> assign ('arr', $ arr );
$ Smarty-> assign ('Nav', $ nav );
$ Smarty-> display('test.html ');
In the smarty template, recursion is as follows:

The code is as follows:




<{Foreach $ nav as $ n}>
<{If $ n @ iteration! = $ N @ last}>
<{$ N. name}>->
<{Else}>
<{$ N. name}>
<{/If}>
<{/Foreach}>




<{Function name = menu}>


    <{Foreach $ data as $ entry}>

  • <{$ Entry. name}> <{* Note that the field must be changed to your own field. *}>
    <{If isset ($ entry. child)}>
    <{Call name = menu data = $ entry. child}>
    <{/If}>

  • <{/Foreach}>

<{/Function}>
<{Call name = menu data = $ arr}> <{* Note that $ arr is the template variable *}>



Of course, you can also change the recursive method and use the tags you want. The recursive method of mixed HTML + PHP compilation is not pasted here. I am too lazy to write it. I hate mixed compilation and look disgusting. here I recommend the SpeedPHP framework of previous generation of Jackes, because the default engine is smarty, My PHP unlimited classification is fully compatible with the SP framework. Similarly, jquery's treeview plug-in and drop-down menu plug-in are also perfectly supported.
By the way, it is recommended that you use the powerful cache function of Smarty, and the cache is king.

Success ,...

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.