Home  >  Article  >  Backend Development  >  PHP uses recursive method to achieve unlimited classification and generate drop-down list

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

怪我咯
怪我咯Original
2017-07-07 09:50:121245browse

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; 
}


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!

Statement:
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