PHP implementation of infinite class classification (without recursion) _php techniques

Source: Internet
Author: User

The infinite level classification is used frequently in the development, for example: Department structure, article classification. The difficulty with the infinite-level classification is the output and the query, for example

    • The output of the article is classified as a <ul> list form;
    • Find articles contained in all categories under Category A.

1. Principle of realization
Several common implementation methods, each have advantages and disadvantages. The "Improved sequence traversal tree" data structure is easy to output and query, but it is somewhat complicated in mobile classification and conventional understanding.

2. Data structure

<?php
 $list = Array (
 ' id ' =>1, ' fid ' =>0, ' title ' => ' China '), 
 array (' ID ' =>2, ' FID ' =>1 , ' title ' => ' Jiangsu '),
 array (' ID ' =>3, ' fid ' =>1, ' title ' => ' Anhui '),
 array (' ID ' =>4, ' fid ' =>8, ' Title ' => ' Jiangyin '),
 array (' ID ' =>5, ' fid ' =>3, ' title ' => ' Wuhu '),
 array (' ID ' =>6, ' fid ' =>3, ' Title ' => ' Hefei '),
 array (' ID ' =>7, ' fid ' =>3, ' title ' => ' Bengbu '),
 array (' ID ' =>8, ' fid ' =>8, ' Title ' => ' Wuxi ')
 );
? >

The classification tree is formed by the level "concatenation" between the categories through the parent class ID (i.e. FID). It is worth noting in tandem: the FID of Category A may not be the ID of its subclass.

In the use of this data structure to output the most commonly used algorithm is "recursion", familiar with the PHP language friends must know that PHP is not good at recursion, and the number of recursion is limited (100 times, due to operating system and configuration vary).

Since all recursion can be implemented using loops, this paper writes a set of functions of "infinite-level" classification according to the features of PHP language, which is more efficient than recursive implementations.

3. Output UL List form
Export the above data to the following HTML

<ul>
 <li class= "First-child" >
 <div> Jiangsu </div>
 <ul>
 <li class= " First-child last-child ">
 <div> wuxi </div>
 <ul>
 <li class=" First-child last-child ">
 <div> jiangyin </div>
 </li>
 </ul>
 </li> </ul> </li>
 <li class= "Last-child" >
 <div> Anhui </div>
 <ul>
 <li class= "First-child" ><div> wuhu </div></li>
 <li><div> Hefei </div></li>
 <li class= "Last-child" ><div> Bengbu </div></li>
 </ul>
 </li>
</ul>

This HTML structure is handy for front-end use (using JavaScript and CSS to construct a collapsible tree). The specific implementation procedures are as follows:

<ul><?php Echo Get_tree_ul ($list, 1);?></ul>

4. Output Option List form

<select>
 <option value= "2" > Jiangsu </option>
 <option value= "8" > Wuxi </option>
 <option value= "4" > Jiangyin </option>
 <option value= "3" > Anhui </option>
 <option value= "5" &    Gt Wuhu </option>
 <option value= "6" > Hefei </option>
 <option value= "7" > Bengbu </option>< C11/></select>

The specific implementation procedures are as follows:

<select>
<?php
 //Get_tree_option () returns the array and adds a "depth" (i.e. depth) column for each element, which can be directly output
 $options = Get_tree_ Option ($list, 1); 
 foreach ($options as $op) {
 echo ' <option value= '. $op [' id ']. ' " > '. Str_repeat ("", $op [' depth '] * 4). $op [' title ']. ' <;/option> ';
>
<;/select>

5. Find all subclasses of a category

<?php
 $children = get_tree_child ($list, 0);
 echo implode (', ', $children); Output: 1,3,2,7,6,5,8,4
?>

6. Find all the parent classes of a category

<?php
 $children = Get_tree_parent ($list, 4);
 echo implode (', ', $children); 8, 2,
?>

7. Correlation function

<?php function Get_tree_child ($data, $fid) {$result = array ();
 $fids = Array ($FID);
 do {$cids = array ();
 $flag = false;
 foreach ($fids as $fid) {for ($i = count ($data)-1; $i >=0; $i-) {$node = $data [$i];
 if ($node [' fid '] = = $fid) {array_splice ($data, $i, 1);
 $result [] = $node [' id '];
 $cids [] = $node [' id '];
 $flag = true;
 }}} $fids = $cids;
 while ($flag = = true);
return $result;
 function Get_tree_parent ($data, $id) {$result = array ();
 $obj = Array ();
 foreach ($data as $node) {$obj [$node [' id ']] = $node; } $value = Isset ($obj [$id])? 
 $obj [$id]: null;
 while ($value) {$id = null;
 foreach ($data as $node) {if ($node [' id '] = = $value [' FID ']) {$id = $node [' id '];
 $result [] = $node [' id '];
 Break
 } if ($id = = null) {$result [] = $value [' FID ']; } $value = Isset ($obj [$id])?
 $obj [$id]: null;
 } unset ($obj);
return $result;
 function Get_tree_ul ($data, $fid) {$stack = array ($FID);
 $child = Array ();
 $added _left = Array (); $added_right= Array ();
 $html _left = Array ();
 $html _right = Array ();
 $obj = Array ();
 $loop = 0;
 foreach ($data as $node) {$pid = $node [' FID '];
 if (!isset ($child [$pid])) {$child [$pid] = array ();
 } array_push ($child [$pid], $node [' id ']);
 $obj [$node [' id ']] = $node;
 while (count ($stack) > 0) {$id = $stack [0];
 $flag = false; $node = Isset ($obj [$id])?
 $obj [$id]: null;
 if (Isset ($child [$id])) {$cids = $child [$id];
 $length = count ($cids);
 for ($i = $length-1; $i >= 0; $i-) {Array_unshift ($stack, $cids [$i]);
 } $obj [$cids [$length -1]][' islastchild '] = true;
 $obj [$cids [0]][' isfirstchild '] = true;
 $flag = true; } if ($id!= $fid && $node &&!isset ($added _left[$id)) {if (Isset ($node [' Isfirstchild ']) && ISS
 ET ($node [' Islastchild '])) {$html _left[] = ' <li class= ' first-child last-child ' > ';
 else if (isset ($node [' Isfirstchild '])) {$html _left[] = ' <li class= ' first-child ' > '; else if (isset ($node [' Islastchild '])} {$html _left[] = ' <li class= ' last-child ' > ';
 else {$html _left[] = ' <li> '; } $html _left[] = ($flag = = True)?
 "<div>{$node [' title ']}</div><ul>": "<div>{$node [' title ']}</div>";
 $added _left[$id] = true; } if ($id!= $fid && $node &&!isset ($added _right[$id)) {$html _right[] = ($flag = = True)?
 ' </ul></li> ': ' </li> ';
 $added _right[$id] = true;
 } if ($flag = = False) {if ($node) {$cids = $child [$node [' FID ']];
 for ($i = count ($cids)-1; $i >= 0; $i-) {if ($cids [$i] = = $id) {array_splice ($child [$node [' FID ']], $i, 1);
 Break
 } if (count ($child [$node [' fid ']] = = 0) {$child [$node [' fid ']] = null;
 } array_push ($html _left, Array_pop ($html _right));
 Array_shift ($stack);
 } $loop + +;
 if ($loop > 5000) return $html _left;
 } unset ($child);
 Unset ($obj);
Return implode (", $html _left);
 function Get_tree_option ($data, $fid) {$stack = array ($FID);
 $child = Array ();
 $added = Array (); $options = Array ();
 $obj = Array ();
 $loop = 0;
 $depth =-1;
 foreach ($data as $node) {$pid = $node [' FID '];
 if (!isset ($child [$pid])) {$child [$pid] = array ();
 } array_push ($child [$pid], $node [' id ']);
 $obj [$node [' id ']] = $node;
 while (count ($stack) > 0) {$id = $stack [0];
 $flag = false; $node = Isset ($obj [$id])?
 $obj [$id]: null;
 if (Isset ($child [$id])) {for ($i = count ($child [$id])-1; $i >= 0; $i-) {Array_unshift ($stack, $child [$id] [$i]);
 } $flag = true;
 } if ($id!= $fid && $node &&!isset ($added [$id])) {$node [' depth '] = $depth;
 $options [] = $node;
 $added [$id] = true;
 } if ($flag = = True) {$depth + +; else {if ($node) {for ($i = count ($child [$node [' FID ']])-1; $i >= 0; $i-) {if ($child [$node [' FID ']][$i] = = $id
 {Array_splice ($child [$node [' FID ']], $i, 1);
 Break
 } if (count ($child [$node [' fid ']] = = 0) {$child [$node [' fid ']] = null;
 $depth--;
 } array_shift ($stack);
 } $loop + +; if ($loop > 5000) return$options;
 } unset ($child);
 Unset ($obj);
return $options;
 }?>

The above introduction is not the use of recursion in the case of PHP to achieve unlimited polar classification, I hope to help you learn.

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.