A PHP class tree (supports unlimited classification)

Source: Internet
Author: User


I recently asked a lot of questions about the infinite classification class tree, so I'm glad I wrote one myself. I just wrote it. You can use it to see how it can be implemented faster and simpler, paste your tree (only one database query is required) <br>

This is a category tree in the category list and supports unlimited classification. <br>
A category can contain both "Classification containing subclasses" and "final classification". <br>

The only advantage is that you only need to perform a database query once. <br>

It looks pretty bad, but it can be customized and modified. You can customize CSS and add it. <br>

The cache has not been completed. You can add it by yourself.

The directory structure in the following example is as follows.
Usage -- catagory. php <br>
Topology -- images ---- tree. jsp <br>
Response --images----treeopen.gif <br>
Response --images----treeclose.gif <br>
Export --images----line.gif <br>

/***************** Tree. JSP ********************/
Function expand (ID ){
Node = Document. All ('node' + id );
If (node. style. Display = ''){
Node. style. Display = 'none ';
Document. Images ('img '+ id). src = imgopen;
} Else {
Node. style. Display = '';
Document. Images ('img '+ id). src = imgclose;
}
}

/***************** Catagory. PHP ********************/
<? PHP

Define ('catagory _ tree_expend_none ', 0 );
Define ('catagory _ tree_expend_all ', 1 );

Class catagory {
// Basic classification data
VaR $ treedata = array ();
// Classification level structure array, with the classification id value as the key word of the array
VaR $ treeplist = array ();
// Relationship between the self-classification class and the parent class
VaR $ treeclist = array ();
/*
* This is a big classification template.
*
* _ Id__ Category Number
* _ Name _ category name
* _ Image _ name of the image displayed before classification $ imgopen or $ imgclose
* _ Open _ indicates whether the category is expanded.
* _ Inner _ Position of subcategory display
* Styles can be modified as needed, but existing elements cannot be deleted.
*/
VaR $ blocktpl ='
<Table border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
<TD colspan = "2"> <a onclick = "Expand (_ id _); Return false;" href = "#">
</>
<A onclick = "Expand (_ id _); Return false;" href = "#">
_ Name __</A> </TD>
</Tr>
<Tr id = "Node _ id _" style = "display :__ open __;">
<TD width = "20"> </TD> <TD >__ inner __</TD>
</Tr>
</Table> ';
/*
* This is a small classification template.
*
* See $ blocktpl
*/
VaR $ elementtpl = ' <a href = "? Id =__ ID _ "> <font color =" white ">__ name __</font> </a> <br/> ';
/*
* This is the current position display template.
*
* See $ blocktpl
*/
VaR $ currenttpl = '<a href = "? Id =__ ID _ "> <font color =" white ">__ name __</font> </a> ';
VaR $ JS = "images/tree. js ";

VaR $ imgopen = 'images/treeopen.gif ';
VaR $ imgclose = 'images/treeclose.gif ';
VaR $ imgline = 'images/line.gif ';

VaR $ cachfile = '';
VaR $ expand = 0;

VaR $ result = array ();
VaR $ treestr = '';
VaR $ currentstr = '';
/*
* Used for initialization and passed in classified data
*
* Param $ data array ()
*/
Function catagory (& $ data ){
$ This-> _ Init ($ data );
}

function _ Init ($ tmpdata) {
$ plevel = $ clevel = $ treedata = array ();
foreach ($ tmpdata as $ value) {
$ treedata [$ value ['id'] = $ value;
$ plevel [$ value ['pid'] [$ value ['id'] = 'end ';
$ clevel [$ value ['id'] = $ value ['pid '];
}< br> $ this-> treedata = & $ treedata;
$ this-> treeplist = & $ plevel;
$ this-> treeclist = & $ clevel;
}< br>/*
* Resolution category list
* Param $ ID of the primary category to be parsed by cataid int
*/
function parsenode ($ cataid = 0) {
$ this-> result = $ this-> treeplist [$ cataid];
if ($ this-> result = NULL) die ("catagory ID error");
$ this-> treestr = $ this-> _ donode ($ this-> result );
$ this-> treestr. = $ this->_jsparse ();
}

Function & _ donode (& $ result ){
$ NSTR = $ estr = '';
Foreach ($ result as $ key => $ value ){
If (isset ($ this-> treeplist [$ key]) {
$ Result [$ key] = $ this-> treeplist [$ key];
$ Inner = $ this-> _ donode ($ result [$ key]);
$ NSTR. = $ this-> _ parsenodetpl ($ key, $ inner );
} Else {
$ Estr. = $ this-> _ parseelementtpl ($ key );
}
}
Return $ NSTR. $ estr;
}

Function & _ parsenodetpl ($ cataid, $ inner ){
$ DATA = $ this-> treedata [$ cataid];
$ STR = preg_replace ('your _ id _ region', $ data ['id'], $ this-> blocktpl );
$ STR = preg_replace ('your _ name _ secret', $ data ['name'], $ Str );
$ STR = preg_replace ('prop _ image _ replation', ($ this-> Expand? $ This-> imgclose: $ this-> imgopen), $ Str );
$ STR = preg_replace ('prop _ open _ replation', ($ this-> Expand? '': 'None'), $ Str );
$ STR = preg_replace ('your _ inner _ region', $ inner, $ Str );
Return $ STR;
}

Function _ parseelementtpl ($ cataid ){
$ DATA = $ this-> treedata [$ cataid];
$ STR = preg_replace ('your _ id _ region', $ data ['id'], $ this-> elementtpl );
$ STR = preg_replace ('your _ name _ secret', $ data ['name'], $ Str );
$ STR = preg_replace ('your _ image _ users', $ this-> imgline, $ Str );
Return $ STR;
}
Function _ jsparse (){
$ STR = "<script language = \" javascript \ ">
Imgopen = \ "$ this-> imgopen \";
Imgclose = \ "$ this-> imgclose \";
</SCRIPT> <SCRIPT src = \ "$ this-> JS \" Language = \ "javascript \"> </SCRIPT> ";
Return $ STR;
}
/*
* Expand category $ cataid
*
* Param $ cataid int indicates the number of the category to be expanded.
*/
Function parsecurrent ($ cataid ){
$ STR = '';
$ Str. = $ this-> _ parsecuratemtpl ($ cataid );
While (isset ($ this-> treeclist [$ cataid]) & $ this-> treeclist [$ cataid]! = 0 ){
$ Cataid = $ this-> treeclist [$ cataid];
$ STR = $ this-> _ parsecuratetpl ($ cataid). '->'. $ STR;
}
$ This-> currentstr = & $ STR;
}

function _ parsecuratemtpl ($ cataid) {
$ DATA = $ this-> treedata [$ cataid];
$ STR = preg_replace ('your _ id _ region', $ data ['id'], $ this-> currenttpl );
$ STR = preg_replace ('your _ name _ secret', $ data ['name'], $ Str);
return $ STR;
}< br>/*
* parse the path of the current Category
* Param $ cataid int indicates the number of the primary category to be parsed.
*/
function expand ($ cataid) {
if ($ this-> Expand> 0) return;
$ STR = '';
I F (isset ($ this-> treeplist [$ cataid]) $ Str. = "Expand ($ cataid);";
while (isset ($ this-> treeclist [$ cataid]) & $ this-> treeclist [$ cataid]! = 0) {
$ Str. = "Expand (". $ this-> treeclist [$ cataid]. ");";
$ cataid = $ this-> treeclist [$ cataid];
}< br> $ this-> treestr. = " ";
}< br>/*
* return the path of the current Category
*/
function getcurrentstr () {
return $ this-> currentstr;
}< br>/*
* return the category tree
*/
function gettreestr () {
return $ this-> treestr;
}

Function settpl ($ blocktpl, $ elementtpl, $ currenttpl, $ JS ){
$ This-> blocktpl = $ blocktpl;
$ This-> elementtpl = $ elementtpl;
$ This-> currenttpl = $ currenttpl;
$ This-> JS = $ JS;
}

Function setimage ($ open, $ close, $ line ){
$ This-> imgopen = $ open;
$ This-> imgclose = $ close;
$ This-> imgline = $ line;
}

Function setexpend ($ expand ){
$ This-> Expand = $ expand;
}

}

// The basic data of classification is as follows:
$ DATA = array (Array ('id' => 1, 'name' => 'name1', 'pid '=> 0, 'order' => 1 ),
Array ('id' => 2, 'name' => 'name2', 'pid '=> 1, 'order' => 1 ),
Array ('id' => 3, 'name' => 'name3', 'pid '=> 0, 'order' => 1 ),
Array ('id' => 4, 'name' => 'name4', 'pid '=> 3, 'order' => 1 ),
Array ('id' => 5, 'name' => 'name5', 'pid '=> 6, 'order' => 1 ),
Array ('id' => 6, 'name' => 'name6', 'pid '=> 2, 'order' => 1 ),
Array ('id' => 7, 'name' => 'name7', 'pid '=> 6, 'order' => 1 ),
Array ('id' => 8, 'name' => 'name8', 'pid '=> 3, 'order' => 1 ),
Array ('id' => 9, 'name' => 'name9', 'pid '=> 6, 'order' => 1 ),
Array ('id' => 10, 'name' => 'name10', 'pid '=> 0, 'order' => 1 ),
Array ('id' => 11, 'name' => 'name11', 'pid '=> 10, 'order' => 1 ),
Array ('id' => 12, 'name' => 'name12', 'pid '=> 10, 'order' => 1 ),
Array ('id' => 13, 'name' => 'name13 ', 'pid' => 10, 'order' => 1 ),
Array ('id' => 14, 'name' => 'name14', 'pid '=> 12, 'order' => 1 ),
Array ('id' => 15, 'name' => 'name15', 'pid '=> 12, 'order' => 4 ),
);

Echo "<body bgcolor = \" Blue \ "> ";
$ Tree = new catagory ($ data );
Echo "<HR> below is the class tree of the current Category <HR> ";
// $ Tree-> setexpend (1 );
$ Tree-> parsenode (0 );
// $ Tree-> parsenode (1 );
// $ Tree-> Expand (9 );
Echo $ tree-> gettreestr ();

Echo "<HR> below is the path of the current category (Classification Number is 9) <HR> ";
$ Tree-> parsecurrent (9 );
Echo $ tree-> getcurrentstr ();

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.