PHP implements an Infinitus classification graphic tutorial ,. PHP graphic tutorial for Infinitus classification. Generally, the implementation of Infinitus classification uses recursive or iterative methods. let's take a look at the implementation methods in this article. 1. Database Design: PHP graphic tutorial for Infinitus classification,
Generally, the implementation of Infinitus classification uses recursive or iterative methods. let's take a look at the implementation methods in this article.
1. database design:
2. Code:
The code is as follows:
/**
* @ Author koma
* @ Todo PHP Infinitus classification
*/$ Cn = mysql_connect ('localhost', 'root', '') or die (mysql_error ());
Mysql_select_db ('t', $ cn) or die (mysql_error ());
Mysql_query ('set names utf8 ');
/**
* Obtain sub-classes from the top layer step down
* @ 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 before entering the subclass -- $ deep; // depth-1} after exiting from the 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. ='';
}/**
* Obtain the parent class from the subclass level up
* @ 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 order, 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 ,:
Isn't it easy? you can use it directly without the copyright fee ^_^
In general, the implementation of Infinitus classification uses recursive or iterative methods. let's take a look at the implementation methods in this article. 1. Database Design :...