An in-depth analysis of PHP infinite Classification (tree type) _php Tutorial

Source: Internet
Author: User
PHP Unlimited classification, Google will be able to find a lot of relevant information, thinking compared to wind, but also used more is the classification table at least id,pid,name three fields, ID self-added table classification, PID for the parent category, name is the classification name, so that constitutes a tree, such as the next, It's the result set I got from my query classification table.
Copy CodeThe code is as follows:
Simulation of 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 ' = ' ringtone '
),
Array
' ID ' =>13,
' PID ' =>9,
' Name ' = ' popular music '
),
Array
' ID ' =>14,
' PID ' =>9,
' Name ' = ' classical music '
),
Array
' ID ' =>15,
' PID ' =>12,
' Name ' = ' hot 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 ' = ' through class '
),
Array
' ID ' =>21,
' PID ' =>11,
' Name ' = ' Martial Arts class '
),
);
?>

Shavers the wind, but those articles provide an infinite assortment of class-related operations a little bit, directly to the database operation is also encapsulated in. That is, others want to use your class, but also to build the same table with you, real TM disgusting. Because the project to use, so I wrote an infinite class of PHP (also known as the Tree Class), no database operation, only need to instantiate the time to pass in the result set, that is, a tree-shaped array. Re-implement the Leaf method or Navi method to get the desired results, see below the source code, after reading the smarty template engine corresponding template recursive method.
Copy CodeThe code is as follows:
/**
* Tree type Class (unlimited 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 function
*
* @param array $result tree data table result set
* @param array $fields tree 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 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, returning the entire tree by default
*/
Public Function leaf ($id = null) {
$id = ($id = = null)? $this->root: $id;
return $this->tmp[$id];
}
/**
* Navigating one-dimensional arrays
*
* @param integer $id Category ID
* @return Array returns single-line classification until top class
*/
Public function Navi ($id) {
$this->arr = null;
$this->recur_n ($this->result, $id);
Krsort ($this->arr);
return $this->arr;
}
/**
* Scattered one-dimensional arrays
*
* @param integer $id Category ID
* @return Array returns all category IDs under leaf
*/
Public Function Leafid ($id) {
$this->arr = null;
$this->arr[] = $id;
$this->recur_p ($this->leaf ($id));
return $this->arr;
}
}
?>

How to use the infinite classification of PHP in Smarty:
$result = $db->query (...); /Here the query gets the result set, note 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 ');
In the Smarty template, this is recursive:
Copy CodeThe code is as follows:


<{foreach $nav as $n}>
<{if $n @iteration! = $n @last}>
<{$n .name}>
<{else}>
<{$n .name}>
<{/if}>
<{/foreach}>



<{function name=menu}>

      <{foreach $data as $entry}>

    • <{$entry .name}> <{* Note the fields to be changed to their own fields Oh *}>
      <{if isset ($entry. Child)}>
      <{call name=menu data= $entry .child}>
      <{/if}>

    • <{/foreach}>

<{/function}>
<{call name=menu data= $arr}> <{* Notice here $arr is the template variable *}>


Of course, you can also change the recursive method, with the label you want to be unconstrained. Html+php Mixed Recursive method here does not post, I do not bother to write, the most hated mixed, looking at nausea, here is recommended for Jake predecessor of the speedphp framework, because the default engine is Smarty, my this PHP unlimited classification is fully compatible with the SP framework. Similarly, jquery's TreeView plugin and drop-down menu plugin are also perfectly supported.
By the way, it is recommended to use the Smarty powerful caching function, the cache is kingly.

http://www.bkjia.com/PHPjc/327092.html www.bkjia.com true http://www.bkjia.com/PHPjc/327092.html techarticle PHP Unlimited classification, Google will be able to find a lot of relevant information, thinking compared to wind, but also used more is the classification table at least id,pid,name three fields, ID self-added table classification, ...

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