PHP Infinite Classification (tree Class) in-depth analysis _php instance

Source: Internet
Author: User
Tags smarty template

PHP infinite Classification, Google will be able to find a lot of relevant information, thinking more pull wind, is also used more than the classification table has at least three fields, ID id,pid,name classification, PID for the parent category, name for the category name, so that constitutes a tree, such as the next, Is the result set that I query the classification table.

Copy Code code as follows:

<?php
Simulate PHP infinite Classification query results
Return Array (
Array
' ID ' =>1,
' PID ' =>0,
' Name ' => ' home '
),
Array
' ID ' =>2,
' PID ' =>0,
' Name ' => ' News '
),
Array
' ID ' =>3,
' PID ' =>0,
' Name ' => ' media '
),
Array
' ID ' =>4,
' PID ' =>0,
' Name ' => ' Download '
),
Array
' ID ' =>5,
' PID ' =>0,
' Name ' => ' About us '
),
Array
' ID ' =>6,
' PID ' =>2,
' Name ' => ' Celestial News '
),
Array
' ID ' =>7,
' PID ' =>2,
' Name ' => ' Overseas News '
),
Array
' ID ' =>8,
' PID ' =>6,
' Name ' => ' state official news '
),
Array
' ID ' =>9,
' PID ' =>3,
' Name ' => ' music '
),
Array
' ID ' =>10,
' PID ' =>3,
' Name ' => ' movie '
),
Array
' ID ' =>11,
' PID ' =>3,
' Name ' => ' novel '
),
Array
' ID ' =>12,
' PID ' =>9,
' Name ' => ' ringtones '
),
Array
' ID ' =>13,
' PID ' =>9,
' Name ' => ' pop music '
),
Array
' ID ' =>14,
' PID ' =>9,
' Name ' => ' classical music '
),
Array
' ID ' =>15,
' PID ' =>12,
' Name ' => ' popular ringtones '
),
Array
' ID ' =>16,
' PID ' =>12,
' Name ' => ' Funny ringtones '
),
Array
' ID ' =>17,
' PID ' =>12,
' Name ' => ' MP3 ringtones '
),
Array
' ID ' =>18,
' PID ' =>17,
' Name ' => ' 128K '
),
Array
' ID ' =>19,
' PID ' =>8,
' Name ' => ' Entertainment News '
),
Array
' ID ' =>20,
' PID ' =>11,
' Name ' => ' Cross class '
),
Array
' ID ' =>21,
' PID ' =>11,
' Name ' => ' Martial arts class '
),
);
?>

La Feng Wind, but those articles provide the infinite classification of the class-related operations a bit of a setback, directly to the database operations are encapsulated. That is, other people want to use your class, but also with you to build the same table, really TM disgusting. Because the project to use, so I wrote a PHP Infinite classification class (also known as Tree Class), there is no database operations, only need to instantiate the time to pass the result set, that is, a tree-shaped array. Then perform the Leaf method or Navi method to get the desired results, please look at the source below, after reading the Smarty template engine on the corresponding template recursive method.
Copy Code code as follows:

<?php
/**
* Tree type (infinite classification)
*
* @author kvoid
* @copyright http://kvoid.com
* @version 1.0
* @access Public
* @example
* $tree = new tree ($result);
* $arr = $tree->leaf (0);
* $nav = $tree->navi (15);
*/
Class Tree {
Private $result;
Private $tmp;
Private $arr;
Private $already = Array ();
/**
* Constructor
*
* @param array $result The result set of the tree data table
* @param array $fields A tree-type data table field, array (category ID, parent ID)
* @param integer $root The parent ID of the top-level category
*/
Public function __construct ($result, $fields = array (' ID ', ' pid '), $root = 0) {
$this->result = $result;
$this->fields = $fields;
$this->root = $root;
$this->handler ();
}
/**
* Tree-type data table result set processing
*/
Private Function Handler () {
foreach ($this->result as $node) {
$tmp [$node [$this->fields[1]]][] = $node;
}
Krsort ($TMP);
for ($i = count ($tmp); $i > 0; $i-) {
foreach ($tmp as $k => $v) {
if (!in_array ($k, $this->already)) {
if (! $this->tmp) {
$this->tmp = Array ($k, $v);
$this->already[] = $k;
Continue
} else {
foreach ($v as $key => $value) {
if ($value [$this->fields[0]] = = $this->tmp[0]) {
$tmp [$k] [$key] [' child '] = $this->tmp[1];
$this->tmp = Array ($k, $tmp [$k]);
}
}
}
}
}
$this->tmp = null;
}
$this->tmp = $tmp;
}
/**
* Reverse Recursion
*/
Private Function Recur_n ($arr, $id) {
foreach ($arr as $v) {
if ($v [$this->fields[0]] = = $id) {
$this->arr[] = $v;
if ($v [$this->fields[1]]!= $this->root) $this->recur_n ($arr, $v [$this->fields[1]]);
}
}
}
/**
* Forward recursion
*/
Private Function Recur_p ($arr) {
foreach ($arr as $v) {
$this->arr[] = $v [$this->fields[0]];
if ($v [' Child ']) $this->recur_p ($v [' child ']);
}
}
/**
* Menu Multidimensional Array
*
* @param integer $id Category ID
* @return Array returns the branch and returns the entire tree by default
*/
Public Function leaf ($id = null) {
$id = ($id = = null)? $this->root: $id;
return $this->tmp[$id];
}
/**
* Navigating a one-dimensional array
*
* @param integer $id Category ID
* @return Array returns single line classification until top classification
*/
Public function Navi ($id) {
$this->arr = null;
$this->recur_n ($this->result, $id);
Krsort ($this->arr);
return $this->arr;
}
/**
* Scattered one-dimensional array
*
* @param integer $id Category ID
* @return Array returns all category IDs under the leaf
*/
Public Function Leafid ($id) {
$this->arr = null;
$this->arr[] = $id;
$this->recur_p ($this->leaf ($id));
return $this->arr;
}
}
?>

How to use PHP's infinite classification in Smarty:
$result = $db->query (...); /Here the query gets the result set, noting that the result set is an array
$tree = new Tree ($result);
$arr = $tree->leaf (0);
$nav = $tree->navi (15);
$smarty->assign (' arr ', $arr);
$smarty->assign (' Nav ', $nav);
$smarty->display (' test.html ');
This recursion in the Smarty template:
Copy Code code as follows:

<!--navigation-->
<div id= "Navigator" >
<{foreach $nav as $n}>
<{if $n @iteration!= $n @last}>
<{$n .name}>->
<{else}>
<{$n .name}>
<{/if}>
<{/foreach}>
</div>
<!--tree Menu-->
<div id= "Menu" >
<{function name=menu}>
<ul>
<{foreach $data as $entry}>
<li>
<span><{$entry .name}></span> <{* Note the field to be changed to its own field Oh *}>
<{if isset ($entry. Child)}>
<{call name=menu data= $entry .child}>
<{/if}>
</li>
<{/foreach}>
</ul>
<{/function}>
<{call name=menu data= $arr}> <{* Note Here $arr is the template variable *}>
</div>

Of course, you can also change the recursive method, with the label you want to be free from constraint. Recursive method of html+php mixing here is not posted, I am too lazy to write, the most annoying mixed, look at nausea, here recommend Jake Predecessors of the speedphp framework, because the default engine is Smarty, my PHP Unlimited classification completely compatible with the SP framework. Similarly, jquery's TreeView plugin and Pull-down menu plugin are also perfectly supported.
By the way, it is recommended to use the Smarty powerful caching function, caching is kingly.

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.