Home > Backend Development > PHP Tutorial > PHP code example to use array to return infinite categories of list data

PHP code example to use array to return infinite categories of list data

怪我咯
Release: 2023-03-13 06:10:01
Original
1103 people have browsed it

phpUsage of custom functionArrayReturns list data of infinite categories. This implementation can improve the efficiency of execution without reading from the database every time Get data.

The code is as follows:

/*—————————————————— */ 
//– 获取无限分类的列表数据 
/*—————————————————— */ 
function get_sort ($parent_id=0,$n=-1) 
{ 
global $db; 
static $sort_list = array (); 
$sql = "SELECT * FROM ".$db->table('article_sort')." WHERE `parent_id` = '{$parent_id}'"; 
$res = $db->query ($sql); 
if ($res) 
{ 
$n++; 
while ($row = $db->fetch_assoc ($res)) 
{ 
$sql = "SELECT * FROM ".$db->table('article_sort')." WHERE `parent_id` = '{$row['sort_id']}'"; 
$children = $db->num_rows ($sql); 
$row['sort_name'] = str_repeat (' ',$n*4).$row['sort_name']; 
$row['children'] = $children; 
$sort_list[] = $row; 
get_sort ($row['sort_id'],$n); 
} 
} 
return $sort_list; 
}
Copy after login

The above is the detailed content of PHP code example to use array to return infinite categories of list data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template