Php implementation code for unlimited classification (recursive method)

Source: Internet
Author: User
Recently, a website requires an infinitely hierarchical drop-down list, so the following works are available. At first, it seems very difficult to think of such a function. I have also done an encyclopedia before, and it also involves the classification function, but it is not an infinitely level classification, instead, we simply implemented a fixed three-level classification. at that time, we designed our own design and thought that the implementation method was too good. In fact, the three-level classification is just a special case of an unlimited classification. After a period of consideration, I had some eyebrows and checked on the internet. it turned out that such things were overwhelming. In fact, the unlimited drop-down list function is very simple, just use a recursive algorithm.
First, we need to design a database. we need to create a table that stores 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:
Category level 1
Category-two level
─ ── Second-level classification B
Category 1 Class B
Then there is the program. here, PHP is used as the description language, which can be easily changed to other languages. because the principle is similar, it is just a recursion.
The code is as follows:
$ Dbhost = "localhost"; // database host name
$ Dbuser = "root"; // database username
$ Dbpd = "123456"; // database password
$ Dbname = "test"; // database name
Mysql_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']);
}
}
?>

This recursive method is a burden on a large number of sub-topics, some mature cms systems. Both production arrays facilitate calling and greatly improve efficiency.

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.