Analysis on the principle of PHP unlimited Classification

Source: Internet
Author: User

For beginners, PHP still has many things to explore in depth. Only by constantly handling problems can we master the sincerity. What is PHP unlimited classification? Just like creating a folder in windows, you can create another folder under the new folder, so that the infinite loop goes down, and the infinite classification is also like this, the parent class can be divided into its subclass, sub-classes can also be divided into sub-classes, so that the infinite loop continues.

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, in turn, as long as 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 PHP unlimited classification function.

// Create a table "class"
Create table 'class '(
'Id' int (11) not null auto_increment COMMENT 'category id ',
'F _ id' int (11) not null comment 'parent id ',
'Name' varchar (25) collate gbk_bin not null comment 'category name ',
Primary key ('id ')
) ENGINE = InnoDB default charset = gbk COLLATE = gbk_bin AUTO_INCREMENT = 1;

// First, we insert the 'News' big classification into the database. Because 'News' is the largest category and no parent class exists, I set f_id to 0.
Insert into 'class' ('id', 'f _ id', 'name') VALUES (1, 0, 'News '); // The id field automatically increases without writing a value.

// 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.
Insert into 'class' ('id', 'f _ id', 'name') VALUES (2, 1, 'php News ');

// Then we insert the 'php6. 0' category into the database. The id of its parent class 'php News' is 2, so its f_id is set to 2.
Insert into 'class' ('id', 'f _ id', 'name') VALUES (3, 2, 'php6. 0 ');

// Similarly, We can insert categories all the time to achieve the unlimited classification of PHP.
// 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;
Insert into 'class' ('id', 'f _ id', 'name') VALUES (4, 0, 'technical ');

// 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.
Insert into 'class' ('id', 'f _ id', 'name') VALUES (5, 4, 'php techno ');

// When we see this, we should all understand how to insert various 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?

 
 
  1. <? Php
  2. Header ("Content-type: text/html; charset = UTF-8 ");
  3. $ Db = new mysqli ("localhost", "root", "", "news_php100 ");
    // Instantiate a database connection. Before using this function, make sure that the mysqli class library has been loaded,
    Or use mysql_connect to connect.
  4. If (mysqli_connect_errno ()){
  5. Echo "link failed:". mysqli_connect_error ();
  6. Exit ();}
  7. $ Db-> query ("set names utf8 ");
  8. $ Result = $ db-> query ("select name from class where f_id = 0 ");
    // Search for the f_id = 0 category, that is, find each category.
  9. While ($ row = $ result-> fetch_assoc ()){
  10. Echo $ row ['name']. "<br>"; // loop every category.
  11. }
  12. // We can loop the subcategories of news.
  13. $ Result = $ db-> query ("select * from class where f_id = 1 ");
    // Search for the classification of f_id = 1, that is, find the subclass of 'News.
  14. While ($ row = $ result-> fetch_assoc ()){
  15. Echo $ row ['name']."
  16. "; // In this way, the subcategory of 'News' is recycled. Note: It is only a subclass, not a Child class.
  17. }
  18. // Write it here and we will find a problem. If this category is of the 10th level, do we need to write
    Are each of the 10 loops repeating? If it is more multi-level classification, it is obviously unrealistic to write.
  19. // What can be done? We can write a recursive function and pass f_id as a parameter,

    Continuously loops the value of each f_id, that is, loops the subclass of each f_id value.
  20. // First, we store the values of each classification in a two-dimensional array, which is useful in the following recursive functions.
  21. $ Result = $ db-> query ("select * from class ");
  22. While ($ row = $ result-> fetch_assoc ()){
  23. $ Arr [] = array ($ row [id], $ row [f_id], $ row [name]); // save one
    CATEGORY id, f_id, and name.
  24. }
  25. Function fenlei ($ f_id = 0) {// $ f_id is initialized to 0, that is, the cycle starts from the maximum classification.
  26. Global $ arr; // declare $ arr as a global variable for reference in the function.
  27. For ($ I = 0; $ I <count ($ arr); $ I ++) {// loops each category.
  28. 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.
  29. Echo $ arr [$ I] [2]. "<br>"; // $ arr [$ I] [1] indicates the name of the $ I + 1 category.
  30. Fenlei ($ arr [$ I] [0]); // $ arr [$ I] [1] indicates the id value of the $ I + 1 category. Recursion
    That is, you can use your id as the f_id parameter to recycle your subclass.
  31. }
  32. }
  33. }
  34. Fenlei (); // use this function.
  35. ?>

The above Sample Code explains the principle and usage of PHP unlimited classification in detail, hoping to help you.


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.