PHP動態生成select標籤

php部分:
function build_select($array,$select='',$showAll=0){
    $tmp = array();
    if($showAll == 1){
        $tmp[] = "<option value='*'>不限</option>";
    }elseif($showAll == 2){
        $tmp[] = "<option value='0'>選擇</option>";
    }

    foreach ((array)$array as $key => $val) {
        if($select == $key){
            $tmp[] = "<option selected value='{$key}'>{$val}</option>";
        }else{
            $tmp[] = "<option value='{$key}'>{$val}</option>";
        }
    }
    return implode("", $tmp);
}

public function index(){
    $cid = intval(I('get.classid'));
    $map = array();
    $arrList = D("School")->where($map)->order("orderval ASC")->getField("id,name");
    $select = funs_build_select($arrList,$cid,1);
    $this->assign('selectClass', $select);
}

html部分:
<select name="school" id="school">
    {$select}
</select>
複製代碼
相關文章
相關標籤/搜索