Unlimited classification-PHP source code

Source: Internet
Author: User
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']);
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.