Phptree--php Fast generation of infinite class classification _php techniques

Source: Internet
Author: User
In the development of tree-shaped data, such as infinite Multilevel classification, is a typical tree-shaped structure. This algorithm, using the recursive idea, in order to simplify the development process, I wrote a tool to improve the development efficiency, the need for friends can refer to the next

It is phptree.

Git address: https://git.oschina.net/jiusem/PHPTree.git

or download http://www.jb51.net/codes/606002.html from the home of the script

The simplest example:

<?php require (' PHPTree.class.php ');//raw data, read from database $data = Array (array (' ID ' =>1, ' name ' = ' book ', ' parent_id ' = >0), array (' ID ' =>2, ' name ' = ' music ', ' parent_id ' =>0), array (' ID ' =>3, ' name ' = ' Book1 ', ' parent_id ' = >1), array (' ID ' =>4, ' name ' = ' book2 ', ' parent_id ' =>3)); $r = Phptree::maketree ($data); Echo Json_encode ($r) ;? >

Output:

[
{
"id": 1,
"Name": "Book",
"parent_id": 0,
"Expanded": false,//Do not expand child nodes
"Children": [
{
"id": 3,
"Name": "Book1",
"parent_id": 1,
"Expanded": false,
"Children": [
{
"id": 4,
"Name": "Book2",
"parent_id": 3,
"Leaf": True
}
]
}
]
},
{
"id": 2,

"Name": "Music",
"parent_id": 0,
"Leaf": True
}
]

The generated data is a tree structure, can be combined with ExtJS and other front-end framework to use. Git contains a ExtJS demo that you can refer to.

Demonstrate:

If you do not need to use the front end frame, just use HTML output, you can use the following method:

$r = phptree::maketreeforhtml ($data);

Get a one-dimensional array, using the Level field to identify the hierarchy of classifications:

Array (  ' id ' =>1,  ' name ' = ' user management ',  ' parent_id ' =>0,  ' level ' =>0//First class category), Array (  ' id ' =>1,  ' name ' = ' user list ',  ' parent_id ' =>1,  ' level ' =>1//two class category ....);

The output is a SELECT tag:

echo ' 

Demonstrate:

Git contains an output of the HTML demo, you can refer to.

With regard to the design of the database, it is only required to ensure that the ID and parent_id fields are included, and other fields can be added by themselves without affecting data generation. PARENT_ID is the parent ID, and if it is a first-level category, it is set to 0. Of course, the fields can also be configured. Keep looking down, I'll show you some advanced ways to use it.

To expand a child node:

Phptree::maketree ($data, Array (' expanded ' + True));

The output data is:

[
{
Id:1,
Name: ' Book1 ',
expanded:true,//Expand child nodes
children:[
...
]
}
]

Custom primary and Parent keys:

Database Readout $data = Array (  ' order_id ' =>1,//primary key  ' name ' = ' Book1 ',  ' pid ' =>0,//Parent key  ...); Phptree::maketree ($data, Array (' primary_key ' = ' order_id ', ' parent_key ' = ' pid '));

The output data is:

[
{
Order_id:1,
Name: ' Book1 ',
pid:0,
...
}
]

The Maketreeforhtml method also supports configuring primary and parent keys.

There are other fields that you can customize, but the following parameters only support the Maketree method:

$r = Phptree::maketree ($data, Array (' expanded_key ' = ' expanded ', ' children_key ' = ' children ', ' Leaf_key '  =& Gt ' Leaf ');

By default, these fields are configured in ExtJS, and if you use the Ztree framework, these fields will need to be reconfigured.

Ztree is a powerful domestic tree frame, it also supports the simple JSON format, is a one-dimensional data format, in fact, there is no need to use the phptree. Of course, the phptree output of the data hierarchy is clear, Ztree is also supported.

Related Article

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.