Principle of unlimited PHP classification

Source: Internet
Author: User
: This article mainly introduces the principle of PHP unlimited classification. if you are interested in the PHP Tutorial, please refer to it. What is unlimited classification? Just like creating a folder in windows, you can create another folder in the new folder, which is infinite. LoopThis is also true for the infinite classification. the parent class can be divided into its sub-classes, and its sub-classes can be divided into its sub-classes, which is always infinite. LoopContinue.

So how does PHP implement its unlimited classification? How can we list all its categories one by one?
First, let's assume there is such a three-level classification: news> PHP News> PHP6.0.
If we want to find the news "PHP6.0 has come out", we can click the news first, and then click the PHP news to find out. that is to say, we can find the news at the top level of my grandfather class, if we know the parent class of a subclass, we can find it. In this way, when designing a database, we can design a parent class id field to implement the unlimited classification function.

// Create a table "class" CREATETABLE 'class' ('id' int (11) NOTNULL auto_increment COMMENT 'Category ID', 'F _ id' int (11) notnull comment 'parent ID', 'name' varchar (25) collate gbk_bin notnull comment 'Category name', PRIMARYKEY ('id ')) ENGINE = MyISAM default charset = gbk COLLATE = gbk_bin AUTO_INCREMENT = 1; // first, we insert the 'news' big classification into the database, because 'news' is the largest classification, there is no parent class above, so I set its f_id to 0. INSERTINTO 'class' ('id', 'F _ id', 'name') VALUES (1, 0, 'news'); // The id field automatically increases, you can leave it empty. // Then we insert the 'php News 'category into the database. the id of its parent class 'news' is 1, so its f_id is set to 1. INSERTINTO 'class' ('id', 'F _ id', 'name') VALUES (2, 1, 'php News '); // then insert 'php6 into the database. 0 indicates the classification. the id of its parent class 'php News 'is 2, so its f_id is set to 2. INSERTINTO 'class' ('id', 'F _ id', 'name') VALUES (3, 2, 'php6. 0 out of '); // similarly, we can continue to insert a category so as to achieve unlimited classification. // We can find that the key to inserting a category is to find the id of the parent class of the category and use it as the value of the f_id field of the category. // Assume that you want to insert the same level of classifier as 'news'. that is to say, it is also the largest classification. if there is no parent class above, its f_id is also set to 0; INSERTINTO 'class' ('id', 'F _ id', 'name') VALUES (4, 0, 'tech '); // There is another classification of 'php' under 'tech'. so how can we insert it? First, find the id of the parent class 'tech' of 'php, and use it as the value of your f_id field. INSERTINTO 'class' ('id', 'F _ id', 'name') VALUES (5, 4, 'php techno'); // see here, we should all know how to insert categories into the database. I will not give an example.

We already know how to insert each category into the database, and how to list each category?


  Query ("set names utf8"); $ result = $ db-> query ("select name from class where f_id = 0"); // query the classification of f_id = 0, that is, to search for each category. While ($ row = $ result-> fetch_assoc () {echo $ row ['name']."
"; // In this way, each categoryLoopCome out .} // We can also subclass NewsLoop. $ Result = $ db-> query ("select * from class where f_id = 1"); // you can find the classification of f_id = 1, that is, the subclass of 'news. While ($ row = $ result-> fetch_assoc () {echo $ row ['name']."
"; // Subcategory of 'news'LoopCome out. Note: it is only a subclass, not a grandson class .} // Write it here and we will find a problem. If this category is of the 10th level, do we need to write 10LoopEach subclassLoopCome out? If it is more multi-level classification, it is obviously unrealistic to write. // What can be done? We can write a recursive function that uses f_id as a parameter and continuouslyLoopThe value of each f_id, that is, the subclass of each f_id valueLoop. // First, we save the values of each category in a two-dimensional array.Recursive functions. $ Result = $ db-> query ("select * from class"); while ($ row = $ result-> fetch_assoc ()) {$ arr [] = array ($ row [id], $ row [f_id], $ row [name]); // Each row stores a category id, f_id, name information .} Functionfenlei ($ f_id = 0) {// $ f_id is initialized to 0, that is, starting from the maximum category.Loop. Global $ arr; // declare $ arrGlobal variablesCan be referenced in a function. For ($ I = 0; $ I Loop. If ($ arr [$ I] [1] = $ f_id) {// $ arr [$ I] [1] indicates the f_id value of the $ I + 1 category. Start with $ f_id = 0, that is, output the f_id = 0 category. Echo $ arr [$ I] [2]."
"; // $ Arr [$ I] [1] indicates the name of the $ I + 1 category. Fenlei ($ arr [$ I] [0]); // $ arr [$ I] [1] indicates the id value of the $ I + 1 category. Recursion, that is, using your id as the f_id parameter and then using its subclass Loop. }}}?>

Http://blog.csdn.net/kao331431214/article/details/5425698

The above introduces the principle of PHP unlimited classification, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.