(Practical article) PHP Recursive implementation of infinite class classification

Source: Internet
Author: User

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.

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.

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:

IF Not EXISTS 'class' (   ' id ' mediumint (NULL auto_increment,   ' title ' varchar (  null,   ' pid ' mediumint (nullDEFAULT ' 0 ',      KEY  (' id ')) ENGINE=myisam  DEFAULT

After inserting the data,

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:

functionGET_STR ($id= 0) {     Global $str; $sql= "Select Id,title from class where pid=$id"; $result=mysql_query($sql);//classification of sub-classes for query PID    if($result&&mysql_affected_rows()){//If there are sub-classes        $str. = ' <ul> ';  while($row=Mysql_fetch_array($result)) {//Loop recordset            $str. = "<li>".$row[' ID ']. "--" .$row[' title ']. "</li>";//Building a StringGET_STR ($row[' ID ']);//call Get_str (), pass the ID parameter in the recordset into the function, and continue querying the subordinate        }         $str. = ' </ul> '; }     return $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:

include_once //  echo//

Effects such as:

Then we'll look at the function that returns the array format, using recursion as well:

functionGet_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 are sub-classes         while($rows=Mysql_fetch_assoc($result)){//Loop recordset            $rows[' list '] = Get_array ($rows[' ID ']);//call the function, pass in the parameter, continue querying the subordinate            $arr[] =$rows;//Combining Arrays        }         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.

include_once //  $list//print_r($list//

Output effects such as:

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

echo json_encode ($list

(Practical article) PHP Recursive implementation of infinite class classification

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.