PHP uses recursive method to achieve unlimited classification and generate drop-down list

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

phpCustom functionRecursionImplementationInfinite classificationGenerate drop-down list, this can improve efficiency, and you don’t have to start from The database reads data.

The code is as follows:

/*—————————————————— */ 
//– 递归实现无限分类生成下拉列表函数 
//– $tpl->assign('sort_list',createSortOptions ()); 
//– $tpl->assign('sort_list',createSortOptions ($sort_id)); 
/*—————————————————— */ 
function createSortOptions ($selected=0,$parent_id=0,$n=-1) 
{ 
global $db; 
$sql = "SELECT * FROM `@article_sort` WHERE `parent_id` = '{$parent_id}'"; 
$options = "; 
static $i = 0; 
if ($i == 0) 
{ 
$options .= &#39;<option value="0″ >请选择</option>&#39;; 
} 
$res = $db->query ($sql); 
if ($res) 
{ 
$n++; 
while ($row = $db->fetch_assoc ($res)) 
{ 
$i++; 
$options .="<option value=&#39;{$row[&#39;sort_id&#39;]}&#39;"; 
if ($row[&#39;sort_id&#39;] == $selected) 
{ 
$options .=&#39; selected &#39;; 
} 
$options .=">".str_repeat(&#39; &#39;,$n*3).$row[&#39;sort_name&#39;]."</option>\n"; 
$options .=createSortOptions ($selected,$row[&#39;sort_id&#39;],$n); 
} 
} 
return $options; 
}
Copy after login


The above is the detailed content of PHP uses recursive method to achieve unlimited classification and generate drop-down list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!