The principle of infinite classification of PHP

Source: Internet
Author: User
What is an infinite classification? Just like creating a new folder under Windows, you can create a new folder under the new folder so that unlimited LoopsDown, infinite classification is also the case, the parent class can be divided into its subclasses, subclasses can also distinguish its subclasses, so that has been unlimited LoopsDown

And how does PHP achieve its infinite classification? How do you list each of its categories?
First we assume that there is a class three classification, news →php News →php6.0 out.
If we are looking for "PHP6.0 out" This news, we first click on the news, and then click on the PHP news can be found out, that is, we can go through the grandfather class level down to find, in turn, we just know a subclass of the parent class, we can find it out. This allows us to design a database with the ability to design a field of the parent class ID to implement an infinitely classified function.

We're building a table, "class." CREATETABLE' class ' ( ' id 'int(one) ' notNULL auto_increment COMMENT ' category ID ', ' f_id 'int(one)  not NULL COMMENT ' parent id ', ' name 'varchar collate gbk_bin  not NULL COMMENT ' category name ', PRIMARYKEY (' id ')) Engine=myisam DEFAULT CHARSET=GBK COLLATE=gbk_bin auto_increment=1 ;First we insert the big category ' News ' into the database, because ' news ' is the largest category, there is no parent, so I set its f_id to 0. INSERTinto' class ' (' id ', ' f_id ', ' name ') VALUES (1, 0, ' News ');ID This field is automatically growing and can not write values. Then we insert the ' PHP News ' category into the database and its parent ' news ' ID is 1, so its f_id is set to 1. INSERTinto' class ' (' id ', ' f_id ', ' name ') VALUES (2, 1, ' PHP News ');Then we insert the ' PHP6.0 out ' category into the database, and its parent class ' PHP News ' ID is 2, so its f_id is set to 2. INSERTinto' class ' (' id ', ' f_id ', ' name ') VALUES (3, 2, ' PHP6.0 out ');In the same way, we can continue to insert the classification, we have reached an infinite classification. We can find that the key to inserting a taxonomy is to find the ID of the parent class of this taxonomy, and then as the value of the f_id field for this taxonomy. Suppose you want to insert a category ' technology ' with ' news ' at the same level, that is, it is also the largest category, there is no parent class, then its f_id is also set to 0; INSERTinto' class ' (' id ', ' f_id ', ' name ') VALUES (4, 0, ' technology ');Under ' technology ' there is another category ' PHP technology ', then how do we insert it, first find the ' PHP technology ' parent class ' technology ' ID, and then as the value of its own f_id field. INSERTinto' class ' (' id ', ' f_id ', ' name ') VALUES (5, 4, ' PHP technology ');See here, presumably everyone should understand how to insert each classification into the database. No longer an example.

We already know how to insert individual classifications into the database, so how do we list the categories?


     Header"Content-type:text/html;charset=utf-8");$db=NewMysqli ("localhost","Root","","news_php100") ;//Instantiate a database connection. Before using this, make sure you have loaded the Mysqli class library, or connect it with mysql_connect. if(Mysqli_connect_errno ()) {Echo"link failed:". Mysqli_connect_error ();Exit(); }$db->query ("Set names UTF8");$result=$db->query ("SELECT name from class where F_id=0");//Find the classification of f_id=0, that is, find each big class.  while($row=$result->FETCH_ASSOC ()) {Echo$row[' name ']."
";//In this way, each large class is recycled . }//We can also recycle the sub-categories of news. $result=$db->query ("SELECT * from class where f_id=1");//Find the classification of f_id=1, that is, to find the sub-class of ' news '. while($row=$result->FETCH_ASSOC ()) {Echo$row[' name ']."
";//So the sub-category of ' news ' is recycled . Note: Only subclasses, not including grandson classes. }//write to here, we will find a question, if this classification is a level 10 classification, do we have to write 10 loops to loop it out each of the sub-classes? If it is more class classification, it is obviously unrealistic to write. //What is the solution? We can write a recursive function, f_id as a parameter, and constantly loop each f_id value, that is to say, each of the f_id value of the sub-class loop out. First we save the values of each category in a two-dimensional array, which is useful in the recursive function below. $result=$db->query ("SELECT * from class"); while($row=$result-&GT;FETCH_ASSOC ()) {$arr[]=Array($row[ID],$row[F_ID],$row[name]);//Each row holds information about the id,f_id,name of a taxonomy. } functionfenlei($f _id=0){//$f _id initialized to 0, which is the start of the cycle from the largest classification.Global$arr;//Declare $arr as a global variable to be referenced in a function. for($i=0;$i <> $arr );$i++){// cycle through each category. if($arr[$i][1]==$f _id){//$arr [$i][1] represents the value of f_id for the $i+1 category. Start $f_id=0, that is, the classification of the f_id=0 output. Echo$arr[$i][2]."
";//$arr [$i][1] represents the value of the name of the $i+1 category. Fenlei ($arr[$i][0]);//$arr [$i][1] represents the value of the ID of the $i+1 category. Recursive, that is, their own ID as the f_id parameter to the recycling of their own sub-class. } }}?>

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

The above introduces the principle of infinite classification of PHP, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.