PHP implements recursive infinite classification, php implements recursive infinite _ PHP Tutorial

Source: Internet
Author: User
PHP implements infinite recursion classification, and php implements infinite recursion. PHP implements infinite recursion classification. php implements infinite recursion in some complex systems. it requires an infinite number of information columns to be classified to enhance the flexibility of the system. So how does PHP implement recursive infinite classification in PHP and implement recursive infinite classification in php?

In some complex systems, it is necessary to classify information columns infinitely to enhance the flexibility of the system. So how does PHP implement unlimited classification? In this article, we use recursive algorithms and mysql data tables to implement unlimited classification.
Recursion, in short, refers to repeated calls of a piece of program code. when the code is written to a custom function, the parameters and other variables are saved, and the function repeatedly calls the function, it does not jump out until a certain condition is reached and the corresponding data is returned.
Mysql
First, we prepare a data table class to record the product category information. The table has three fields: id: category number, primary key auto-increment, title: category name, and pid: Parent Category id.
Class table structure:

CREATE TABLE IF NOT EXISTS `class` (  `id` mediumint(6) NOT NULL AUTO_INCREMENT,  `title` varchar(30) NOT NULL,  `pid` mediumint(6) NOT NULL DEFAULT '0',  PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 

After inserting data,

PHP
Based on different requirements, we provide two types of custom functions in different formats, one is to return a string, the other is to return an array, and both functions use a recursive method. First, return the functions in string format:

Function get_str ($ id = 0) {global $ str; $ SQL = "select id, title from class where pid = $ id"; $ result = mysql_query ($ SQL ); // query the classification of the pid subclass if ($ result & mysql_affected_rows () {// if there is a subclass $ str. ='
 
 
    '; While ($ row = mysql_fetch_array ($ result) {// cyclic record set $ str. ="
  • ". $ Row ['id']." -- ". $ row ['title']."
  • "; // Construct the string get_str ($ row ['id']); // call get_str () to pass the id parameter in the record set to the function, continue to query lower-level} $ str. ='
';} Return $ str ;}

The above function get_str () continuously queries lower-level categories through recursion and returns a string. you can modify the str according to the project requirements to generate an unlimited rating list:

Include_once ('connect. php'); // connect to the database. write an echo get_str (0) in the connect. php file. // output an infinite classification.

The effect is as follows:

Next, let's look at the functions that return the array format, and use recursion as well:

Function get_array ($ id = 0) {$ SQL = "select id, title from class where pid = $ id"; $ result = mysql_query ($ SQL ); // query subclass $ arr = array (); if ($ result & mysql_affected_rows () {// if there is a subclass while ($ rows = mysql_fetch_assoc ($ result )) {// cyclic record set $ rows ['list'] = get_array ($ rows ['id']); // call a function and input a parameter, continue to query lower-level $ arr [] = $ rows; // Combined Array} return $ arr ;}}

The get_array () function returns an array, which we expect. Therefore, we recommend that you use get_array () to obtain the array. in this way, we can perform any operation on the array, for example, we can convert the array into json format data and send it to the front-end page. the front-end page can flexibly display the category information by parsing json data. For example, the tree-like category list and drop-down category list.

Include_once ('connect. php'); // connect to the database $ list = get_array (0); // call the print_r function ($ list); // output an array

The output result is as follows:

To output data in json format, you can use:

echo json_encode($list); 

The above method teaches you how to use php to implement recursive infinite classification. I hope this article will be helpful for your learning.

In some complex systems, it is required to classify information columns infinitely to enhance the flexibility of the system. So how PHP is implemented...

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.