PHP implementation of infinite-pole classification and graphics tutorial,
In general, the realization of infinite pole classification is the use of recursion or iterative approach, the small partners to see the implementation of this article.
1, Database design:
2, Code:
Copy CodeThe code is as follows:/**
* @author Koma
* @todo php Infinite Pole classification
*/$CN = mysql_connect (' localhost ', ' root ', ') or Die (Mysql_error ());
mysql_select_db (' t ', $CN) or Die (Mysql_error ());
mysql_query (' Set names UTF8 ');
/**
* Step down from the top level to get the sub-class
* @param number $pid
* @param array $lists
* @param number $deep
* @return Array
*/function getlists ($pid = 0, & $lists = Array (), $deep = 1) {
$sql = ' SELECT * from category WHERE pid= '. $pid;
$res = mysql_query ($sql);
while (($row = Mysql_fetch_assoc ($res))!== FALSE) {
$row [' catename '] = Str_repeat (", $deep). ' | ---'. $row [' Catename '];
$lists [] = $row;
GetLists ($row [' id '], $lists, + + $deep); Depth +1-$deep before entering subclasses; Depth-1} after exit from subclass
return $lists;
}
function displaylists ($pid = 0, $selectid = 1) {
$result = GetLists ($pid);
$str = ' '; foreach ($result as $item) {$selected = ""; if ($selectid = = $item [' id ']) {$selected = ' selected '; } $str. = ''. $item [' Catename ']. '; } return $str. = '';
} /**
* Start from subclass to get its parent class
* @param number $cid
* @param array $category
* @return Array:
*/function GetCategory ($cid, & $category = Array ()) {
$sql = ' SELECT * from category WHERE id= '. $cid. ' LIMIT 1 ';
$result = mysql_query ($sql);
$row = Mysql_fetch_assoc ($result);
if ($row) {
$category [] = $row;
GetCategory ($row [' pid '], $category);
}
Krsort ($category); Reverse, to achieve the effect from the parent class to the subclass return $category;
}
function DisplayCategory ($cid) {
$result = GetCategory ($cid);
$str = "";
foreach ($result as $item) {
$str. = '. $item [' Catename ']. ' > ';
}
Return substr ($str, 0, strlen ($str)-1);
}
Echo displaylists (0, 3);
Echo displaycategory (13);
3,:
is not very simple, small partners can directly take to use Ha, do not charge royalties ^_^
http://www.bkjia.com/PHPjc/917041.html www.bkjia.com true http://www.bkjia.com/PHPjc/917041.html techarticle PHP implementation of infinite Pole classification and graphics tutorial, in general, the realization of infinite pole classification is the use of recursion or iterative way, small partners look at the implementation of this article. 1, database design: ...