您的当前位置:首页正文

PHP动态生成select标签

2024-11-30 来源:个人技术集锦

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>
复制代码


转载于:https://juejin.im/post/5cecd1d7518825334a390eea

显示全文