A PHP tree tree (support unlimited classification)

zhaozj2021-02-12  250

Recently, I have already asked the unlimited classification tree problem, so I am happy to write one, I just finished, everyone used it, see how to achieve faster, simpler, put your tree is also posted (to Just query one database)
This is a class tree for a classification list, supports unlimited classification
One classification can be "classified" and "final classification" and "final classification";
The only advantage ***** is only required once ***** database queries.
looks not very nice, but you can customize to modify, add css cache can define their own terms of which have not made
, you can own This is the case that the directory structure of the following example is.

| - Catagory.php
| - Images ---- Tree.jsp
| - images ---- Treeopen.gif
| - images ---- Treeclose.gif | - images ---- line.gif
/ ************************************ ***** / function expand (id) {node = document.all ('Node' ID); if (node.style.display == ') {node.style.display =' none '; document. Image ('IMG' ID) .SRC = Imgopen;} else {node.style.display = '; Document.Images (' IMG ' ID) .SRC = imgclose;}} / ******* ********* CATAGORY.PHP ****************************** /

__name__ __ inner __ '; / * * This is a small classification template * * * * SEE $ blocktpl * / var $ elementtpl = ' __ name __

Var $ expestr = array (); var $ treestr = ''; var $ currentstr = ''; / * * Used to initialize, incoming classification 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 = & $ pvel; $ this-> treeclist = & $ clevel;} / * * Resolution Category * * PARAM $ CATAID INT To parse the primary classification number * / function parseode ($ 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 (& $ results) {$ nstr = $ estr = '; foreach ($ results $ key => $ value) {ix ($ f) 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 |__ID__ | ', $ DATA [' ID '], $ this-> blocktpl); $ str = preg_replace (' |__name__ | ', $ data [' name '], $ STR); $ str = preg_replace (' | __IMAGE__ | ($ this-> expand? $ this-> imgclose: $ this-> imgopen), $ STR); $ str = preg_replace ('|__open___ |', ($ this-> expand? '': ' None '), $ STR); $ str = preg_replace (' |__inner__ | ', $ inner, $ str); return $ str;} function _ PARSEELEMENTTPL ($ cataid) {$ data = $ this-> Treedata [$ cataid]; $ str = preg_replace ('|__id___ |', $ data ['id'], $ this-> elementtpl); $ str = preg_replace '|__name__ |', $ data ['Name'], $ STR); $ Str = preg_replace ('|__image__ |', $ this-> imgline, $ str); return $ str;} function _jspival () {$ Str = "