Ec (2); first, you need to design a database tutorial. You need to create a table to store the category information. At least three fields are required. The first one is the primary key (id ), the second is the parent category id (parentid), and the third is the category name (classname ). Possible effects: id & nbsp; parentid & nbsp; classname1 & nbsp; 0 & nbsp; & nbsp; & nbsp script ec (2); script
First, we need to design a database tutorial. We need to create a table to store the category information. At least three fields are required. The first is the primary key (id) and the second is the parent category id (parentid ), the third is the category name (classname ). One possible effect is:
Id parentid classname
1 0 level 1 classification
2 0 level 1 classification B
3 1 Level 2 classification
4 1 Level 2 classification B
Main Idea: First, check the third and fourth rows. The value of the parent class id (parentid) is 1, indicating that it belongs to the subclass of the class id = 1, because the two rows are classified as level-1, and there is no parent category, the value of the parent category id (parentid) is 0, indicating that the primary category, and so on, implements an infinite classification. The final result is:
Level
Level 2 classification
Level 2 classification B
Level B
Then there is the program. Here we use the php tutorial as the description language, which can be easily changed to other languages. Because the principles are similar, it is just a recursion.
$ Dbhost = "localhost"; // Database Host Name
$ Dbuser = "root"; // database username
$ Dbpd = "123456"; // Database Password
$ Dbname = "test"; // Database Name
Mysql tutorial _ connect ($ dbhost, $ dbuser, $ dbpd); // connect to the host
Mysql_select_db ($ dbname); // select a database
Mysql_query ("set names 'utf8 '");
Display_tree ("primary", 0 );
Function display_tree ($ tag, $ classid ){
$ Result = mysql_query ("
Select *
From ylmf_class
Where parentid = '". $ classid ."'
;"
);
While ($ row = mysql_fetch_array ($ result )){
// Display the node name in indentation
Echo $ tag. $ row ['classname']."
";
// Call this function again to display the subnode of the subnode
Display_tree ($ tag. "-rows", $ row ['id']);
}
}
?>