In some complex systems, infinite levels of classification of information columns are required to enhance the flexibility of the system. So how does PHP achieve infinite classification? In this article, we use a recursive algorithm combined with a mysql data table to achieve infinite classification.
Recursion, simply put, is the repeated calling of a piece of program code. When the code is written into a custom function, parameters and other variables are saved, and the function is repeatedly called until a certain condition is reached before jumping out and returning the corresponding data.
Mysql
First, we prepare a data table class to record product classification information. There are three fields in the table, id: classification number, the primary key grows automatically; title: classification name; pid: the id of the superior classification to which it belongs.
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, as shown in the figure:
PHP
According to different needs, we provide two custom functions in different formats, one returns a string and the other returns an array. Both functions use recursive methods. Let’s first look at the function that returns string format:
function get_str($id = 0) { global $str; $sql = "select id,title from class where pid= $id"; $result = mysql_query($sql);//查询pid的子类的分类 if($result && mysql_affected_rows()){//如果有子类 $str .= '<ul>'; while ($row = mysql_fetch_array($result)) { //循环记录集 $str .= "<li>" . $row['id'] . "--" . $row['title'] . "</li>"; //构建字符串 get_str($row['id']); //调用get_str(),将记录集中的id参数传入函数中,继续查询下级 } $str .= '</ul>'; } return $str; }
The above function get_str() continuously queries lower-level categories through recursion, and finally returns a string. You can modify the str according to project needs, and finally generate an infinite hierarchical list:
include_once('connect.php'); //连接数据库,connect.php文件自己写一个啊 echo get_str(0); //输出无限级分类
The effect is as follows:
Then let’s look at the function that returns an array format. We also need to use recursion:
function get_array($id=0){ $sql = "select id,title from class where pid= $id"; $result = mysql_query($sql);//查询子类 $arr = array(); if($result && mysql_affected_rows()){//如果有子类 while($rows=mysql_fetch_assoc($result)){ //循环记录集 $rows['list'] = get_array($rows['id']); //调用函数,传入参数,继续查询下级 $arr[] = $rows; //组合数组 } return $arr; } }
The function get_array() returns an array, which is what we expect, so the author recommends using get_array() to get the array. In this way, we can perform any operation on the array. For example, we can convert the array into json format data. Passed to the front-end page, the front-end page can flexibly display classification information by parsing json data. For example, tree-structured category lists, drop-down category lists, etc.
include_once('connect.php'); //连接数据库 $list = get_array(0); //调用函数 print_r($list); //输出数组
The output effect is as follows:
If you want to output data in json format, you can use:
echo json_encode($list);
The above method teaches you how to use PHP to achieve recursive infinite classification. I hope this article will be helpful to your learning.