<?php
Header ("content-type:text/html; Charset=utf-8 ");
/**
*
* @category Contry_category to achieve an infinite level classification, similar to the division of categories, commonly used in column navigation
*
*/
/**
+-------------------------------------------------------------------------------+
| ID Name Category ID |
| 1 China 0 |
| 2 Beijing 1 |
| 3 Shanghai 1 |
| 4 USA 0 |
* 5 New York 4 *
* 6 Washington 4 *
* 7 California, 4 *
+-------------------------------------------------------------------------------+
*
* Category ID 0 is the top column
* Category ID for the same ID is the category
*/
Class contry_category{
public $i =-1;
Public Function Index ($array) {
$this->i++;
if ($array [$this->i][' category_id ']==0) {
echo "├". $array [$this->i]["name"]. " <br/> ";
for ($j =0; $j < count ($array); $j + +) {
if ($array [$j] [' category_id ']== $array [$this->i][' id ']} {//view category
echo "├─┴". $array [$j] [name]. " <br/> ";
}
}
}
foreach ($array as $v) {
if ($v [' category_id ']==0)
$ary []= $v [' category_id ']; Get several top-level columns to determine recursion several times
}
if ($this->i<=count ($ary))
$this->index ($array); Recursive
}
}
$infos = Array ("id" =>1, ' name ' = ' "China", "category_id" =>0), Array ("id" =>2, ' name ' = ' "Beijing", "Category_ ID "=>1", Array ("id" =>3, ' name ' = = "Shanghai", "category_id" =>1), Array ("id" =>4, ' name ' = "USA", "category_id "=>0", Array ("id" =>5, ' name ' = = "New York", "category_id" =>4), Array ("id" =>6, ' name ' = "Washington", "category_id" =>4), Array ("id" =>7, ' name ' = ' "California forint", "category_id" =>4));
$obj =new contry_category ();
$obj->index ($infos);
/**
* Print out the effect is like this
*├ China
├─┴ Beijing
├─┴ Shanghai
├ USA
├─┴ New York
├─┴ Washington
├─┴, California
*/