PHP implements recursive infinite class classification, PHP implementation recursive infinite
In some complex systems, it is required to classify the information columns infinitely, in order to enhance the flexibility of the system. So how does PHP implement an infinite class classification? We use recursive algorithms in this article and combine MySQL data tables to achieve infinite classification.
Recursive, Simple is a program code repeated calls, when the code is written into a custom function, the parameters and other variables are saved, function repeatedly called functions, 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: Classification number, primary key self-growth; title: category name; PID: 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 ',
After inserting the data,
Php
Depending on the requirements, we provide two custom functions in different formats, one is the return string, the other is the return array, and both functions use a recursive method. 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);//The subclass of the query PID if ($result && mysql_affected_rows ()) {//If there is a subclass $str. = '
'; while ($row = Mysql_fetch_array ($result)) {//cyclic recordset $str. = "
- " . $row [' id ']. "--" . $row [' title ']. "
"; Build The string get_str ($row [' id ']),//Call GET_STR (), pass the ID parameter in the recordset into the function, and continue querying the subordinate } $str. = '
; }
The above function get_str () recursively queries the sub-classification and eventually returns a string, which can be modified according to the requirements of the project, and eventually generate an infinite hierarchical list:
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 sub-class $arr = Array (); if ($result && mysql_affected_rows ()) {//If there is a subclass while ($rows =mysql_fetch_assoc ($result)) {//cyclic Recordset $ rows[' list ' = Get_array ($rows [' id ']); Call the function, pass in the parameter, continue querying the subordinate $arr [] = $rows;//combination array } return $arr;
The function Get_array () returns an array, which is what we expect, so I recommend using Get_array () to get an array, so that we can do anything with arrays, for example, we can convert the array into JSON-formatted data to the front-end page, Front-end pages can flexibly present categorical information by parsing JSON data. such as the tree structure of the classification list, drop-down category list and so on.
Output effects such as:
If you want to output JSON-formatted data, you can use:
The above methods teach you how to use PHP to achieve recursive infinite class classification, I hope this article is helpful to everyone's study.
http://www.bkjia.com/PHPjc/1063906.html www.bkjia.com true http://www.bkjia.com/PHPjc/1063906.html techarticle PHP implements recursive infinite class classification, PHP recursive infinity in some complex systems, the information column is required to be unlimited classification, in order to enhance the flexibility of the system. So how is PHP real ...