The infinitus classification mainly refers to the storage of tables in the database. One is the parent ID, and the other is the child ID used to query the parent-level relationship. Then we want the infinitus classification mainly refers to the storage of tables in the database, one is the parent ID, and the other is the child ID to query the parent-level relationship through them, and then what we want
Script ec (2); script
Table Structure: The id field is the category id, the name field is the category name, The father_id field is the id of the parent category, and the path field is the category path (save this score)
Set of class ancestor), isdir determines whether it is a directory (1 is yes, 0 is no ).
Example 1
| The Code is as follows: |
|
|
// $ Count indicates the classification level
Sort_list ($ str, $ fatherid, $ count)
{
$ Rs = $ this-> SQL-> re_datas ("select * from sort where father_id = fatherid ");
$ Num = $ this-> SQL-> SQL _numrows ();
$ I = 0;
$ N = 1;
While (isset ($ rs [$ I])
{
$ Name = "";
For ($ n = 1; $ n <$ count; $ n ++)
{
$ Name. = "│ ";
}
If ($ I + 1 = $ num)
{
$ Name. = "example-". $ rs [$ I] [name];
}
Else
{
$ Name. = "example-". $ rs [$ I] [name];
}
If ($ rs [$ I] [isdir])
{
$ Str. = "". $ name ."";
}
Else
{
$ Str. = $ name ";
}
$ Temp = $ count + 1;
$ Str = $ this-> sort_list ($ str, $ rs [$ I] [id], $ temp );
$ I ++;
}
Return $ str;
} |
$ This-> the SQL object is an SQL operation object, the re_datas () function returns the queried array, and the SQL _numrows () function returns the queried number.
Call method: $ sort_list = sort_list ($ sort_list, 0, 1 );
Instance 2
Id
Fid parent category ID
Class_name category name
Path classifier path, which uses id as a node to form a string similar to, 1, 2, 3, and 4.
----------------------------
The following data can be assumed:
Id fid class_name path
------------------
1 0 Category 1, 1,
2 0 Category 2, 2,
3 1 Classification 1-1, 1, 3,
4 1 Classification 1-2, 1, 4,
5 2 categories 2-1, 2, 5,
6 4 categories 1-2-1, 1, 4, 6,
------------------
| The Code is as follows: |
|
$ SQL = "SELECT * FROM tree order by path ";
$ Result = $ nbs-> Query ($ SQL );
While ($ rows = $ nbs-> fetch_array ($ result )){
If (substr_count ($ rows ['path'], ',')> 2 ){
For ($ I = 0; $ I <(substr_count ($ rows ['path'], ',')-2); $ I ++)
Echo '';
}
Echo $ rows ['class _ name'].'
';
}
?> |
Code
| The Code is as follows: |
|
|
$ Conn = mysql_connect ('localhost', 'root', 'root ');
Mysql_select_db ('hangzhou123', $ conn );
Mysql_query ('set names utf8 ');
$ SQL = "select id, concat (catpath, '-', id) as abspath, name from category order by abspath ";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
/**
* Method 1
*/
/* $ Space = str_repeat ('', count (explode ('-', $ row ['abspath'])-1 );
Echo $ space. $ row ['name'].'
';*/
/**
Method 2
*/
$ Space = str_repeat ('--', count (explode ('-', $ row ['abspath'])-1 );
$ Option. = ''. $ space. $ row ['name'].'
';
}
Echo $ option;
Exit ();
Echo''. $ Option .''; |