A PHP class tree (unlimited classification supported) _php tutorial

Source: Internet
Author: User
Recently asked the infinite classification of the class tree problem more, so a happy to write a, I just finished, we use to see, see how to achieve faster, more simple, put your tree also posted (to query only once the database)


This is a class tree of category list, support unlimited classification

Under a classification, there can be both "categories containing subcategories" and "final classifications";


The only advantage is that * * * * only need to make a database * * * * query.


Look not very good, but you can customize the changes, you can define the CSS add inside


The cache aspect has not yet been done, can fill up

This is the directory structure for the following example.
¦--catagory.php

¦--images----tree.jsp

¦--images----Treeopen.gif

¦--images----Treeclose.gif

¦--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 ();
An array of hierarchical structures, with categorical ID values as the keywords for the array
var $treePList = array ();
The relationship of self-classification corresponding to superior class
var $treeCList = array ();
/*
* This is a template for large categories
*
* Number of __id__ categories
* Name of the __name__ category
* The image name shown earlier in the __image__ category $imgOpen or $imgClose
* Whether the __OPEN__ classification is currently expanded
* The location of the __INNER__ sub-category * style can be changed according to your own needs, but can not delete existing elements
*/
var $blockTpl = '










__name__
__inner__
';
/*
* This is a template for small categories
*
* See $BLOCKTPL
*/
var $elementTpl = ' __name__
';
/*
* This is the current location display template
*
* See $BLOCKTPL
*/
var $currentTpl = ' __name__ ';
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 to initialize, incoming categorical 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;
}
/*
* Analytic Classification List
*
*param $cataId int the number of the main category to parse
*/
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 (' ¦__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 _jsparse () {
$str = "";
return $str;
}
/*
* Expand Category $cataid
*
*param $cataId int the number of the category to expand
*/
function Parsecurrent ($cataId) {
$str = ";
$str. = $this->_parsecurrenttpl ($cataId);
while (Isset ($this->treeclist[$cataId]) && $this->treeclist[$cataId]!=0) {
$cataId = $this->treeclist[$cataId];
$str = $this->_parsecurrenttpl ($cataId). ' -$str;
}
$this->currentstr = & $str;
}

function _parsecurrenttpl ($cataId) {
$data = $this->treedata[$cataId];
$str = preg_replace (' ¦__id__¦ ', $data [' id '], $this->CURRENTTPL);
$str = preg_replace (' ¦__name__¦ ', $data [' name '], $STR);
return $str;
}
/*
* Parse the path of the current classification
*
*param $cataId int the number of the main category to parse
*/
function expand ($cataId) {
if ($this->expand>0) return;
$str = ";
if (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];
}
$this->treestr. = "";
}
/*
* Returns the path of the current category
*/
function Getcurrentstr () {return $this->currentstr;
}
/*
* Returns the class tree of the taxonomy
*/
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 underlying data for the taxonomy looks like this:
$data = 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 "";
$tree = new catagory ($data);
echo "Below is the class tree of the current classification";
$tree->setexpend (1);
$tree->parsenode (0);
$tree->parsenode (1);
$tree->expand (9);
echo $tree->gettreestr ();

echo "Below is the path to the current classification (category number is 9)";
$tree->parsecurrent (9);
echo $tree->getcurrentstr ();

http://www.bkjia.com/PHPjc/314589.html www.bkjia.com true http://www.bkjia.com/PHPjc/314589.html techarticle recently asked the infinite classification of the class tree problem more, so a happy to write a, I just finished, we use to see, see how to achieve faster, more simple, put your tree also affixed ...

  • 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.