PHP 无限极分类下拉列表实现
程序员文章站
2023-12-01 22:13:16
1. 递归实现下拉列表 ......
1. 递归实现下拉列表
/** * @param 递归 实现下拉列表 分类 */ include('db.inc.php'); function getlist($pid = 0,&$result = array(), $spac = 0){ $spac = $spac + 2; $sql = "select * from deepcate where pid = $pid"; $res = mysql_query($sql); while($row = mysql_fetch_array($res)){ $row['catename'] = str_repeat(' ',$spac).'|--'.$row['catename']; $result[] = $row; getlist($row['id'],$result,$spac); } return $result; } function displaycate($pid=0,$select = 0){ $rs = getlist($pid); $str .= "<select name = 'cate'>"; foreach ($rs as $key => $val) { $selected = ''; if($val['id'] == $selected){ $selected = "selected"; } $str .= "<option {$selected}>{$val['catename']}</option>"; } return $str .= '</select>' } echo displaycate(0);
上一篇: 微信小程序 解析网页内容详解及实例