php遞迴實現無限分類生成下拉列表
阿新 • • 發佈:2019-02-10
php自定義函式之遞迴實現無限分類生成下拉列表,不用每次都從資料庫讀取資料。
程式碼:
程式碼:
/*—————————————————— */ //– 遞迴實現無限分類生成下拉列表函式 //– $tpl->assign('sort_list',createSortOptions ()); //– $tpl->assign('sort_list',createSortOptions ($sort_id)); /*—————————————————— */ //php遞迴函式 http://www.jbxue.com/tags/phpdiguihanshu.html 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 .= '<option value="0″ >請選擇</option>'; } $res = $db->query ($sql); if ($res) { $n++; while ($row = $db->fetch_assoc ($res)) { $i++; $options .="<option value='{$row['sort_id']}'"; if ($row['sort_id'] == $selected) { $options .=' selected '; } $options .=">".str_repeat(' ',$n*3).$row['sort_name']."</option>\n"; $options .=createSortOptions ($selected,$row['sort_id'],$n); } } return $options; }