A php class tree (supports unlimited classification)

Source: Internet
Author: User
A php class tree (supports unlimited classification) recently asked a lot of questions about the class tree of unlimited classification, so I was so happy to write one by myself. I just wrote it and you can use it for reference, see how it can be implemented faster and simpler. paste your tree (only query the database once)


This is a category tree in the category list and supports unlimited classification.

A classification can contain both "classification containing subclasses" and "final classification ";


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


It looks pretty bad, but it can be customized and modified. you can customize css and add it.


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

Tags -- images ---- tree. jsp

Response --images----treeopen.gif

Response --images----treeclose.gif

Export --images----line.gif


/***************** 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 ********************/

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 displayed by subcategory * The style can be modified as needed, but existing elements cannot be deleted.
*/
Var $ blockTpl ='










_ Name __
_ Inner __
';
/*
* This is a small classification template.
*
* See $ blockTpl
*/
Var $ elementTpl = '_ name __
';
/*
* This is the current position display template.
*
* See $ blockTpl
*/
Var $ currentTpl = '_ name __';
Var $ js = "images/tree. js? 7.1.2 ";

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 '];
}
$ This-> treeData = & $ treeData;
$ This-> treePList = & $ plevel;
$ This-> treeCList = & $ clevel;
}
/*
* Parse the category list
*
* Param $ cataId int ID of the primary category to be parsed
*/
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 ="

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.