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