PHP implementation of recursive infinite-level classification _php skills

Source: Internet
Author: User
Tags string format

In some complex systems, it is required to classify the information columns in an infinite level to enhance the flexibility of the system. So how does PHP achieve an infinite level of classification? We use recursive algorithm in this paper and combine the MySQL data table to achieve the infinite classification.
Recursion, simply is a piece of program code repeated calls, when the code to write to a custom function, the parameters such as variables to save, function repeatedly call the function, until a certain condition to jump out, return the corresponding data.
Mysql
First we prepare a data table class to record the product classification information. There are three fields in the table, ID: Category number, primary key from growth; title: category name; PID: the owning parent category ID.
Class Table structure:

CREATE TABLE IF not EXISTS ' class ' ( 
 ' id ' mediumint (6) is not NULL auto_increment, 
 ' title ' varchar is not NULL, 
 ' PID ' Mediumint (6) Not NULL default ' 0 ', 
 PRIMARY KEY (' id ') 
) Engine=myisam DEFAULT Charset=utf8; 

After inserting the data, as shown in the figure:

Php
depending on the requirements, we provide two different forms of custom functions, one is to return the string, one is to return the array, both of the functions use recursive methods. First look at the function that returns the string format:

function get_str ($id = 0) { 
 global $str; 
 $sql = "Select Id,title from class where pid= $id"; 
 $result = mysql_query ($sql);//Query The class of the PID subclass 
 if ($result && mysql_affected_rows ()) {//If there are subclasses 
  $str. = ' < Ul> '; 
  while ($row = Mysql_fetch_array ($result)) {//circular Recordset 
   $str. = "<li>". $row [' id ']. "--" . $row [' title ']. "</li>"; Build string 
   get_str ($row [' id ']);//Call GET_STR (), pass the ID parameter in the recordset to the function, and continue querying the subordinate 
  } 
  $str. = ' </ul> '; 
 return $str; 
} 

The above function get_str () through recursion, continuously inquires the subordinate classification, and finally returns the string, everybody can revise the STR according to the project demand, finally produces an infinite rank list:

Include_once (' connect.php '); Connect database, connect.php file write one yourself. 
echo get_str (0);//Output Infinite class classification 

Effects such as:

Then we'll look at the function that returns the array format, using 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 are subclasses while 
  ($rows =mysql_fetch_assoc ($result)) {//circular Recordset 
   $ rows[' list ' = Get_array ($rows [' id ']); Call the function, pass in the parameter, and continue querying the subordinate 
   $arr [] = $rows;//composite Array 
  } return 
  $arr; 
 } 
} 

function Get_array () returned the array, which is what we expected, so the author recommends using Get_array () to get the array, so that we can do any operation on the array, for example, we can convert arrays into JSON-formatted data to the front page, The front-end page can display the classification information flexibly by parsing the JSON data. For example, the tree structure of the classification list, Drop-down category list, and so on.

Include_once (' connect.php '); Connection database 
$list = get_array (0);//Call Function 
Print_r ($list);//output array 

Output effects such as:

If you want to output JSON-formatted data, you can use:

 
 

The above method teaches everybody how to use PHP to realize the recursive infinite class classification, hoped this article is helpful to everybody's study.

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.