yii2 下拉菜单
model
public static function getCatlist(){
$cat = ['0' => '暂无分类'];
$res = self::find()->asArray()->all();
//print_r($res);die;
if($res){
foreach ($res as $k => $list) {
$cat[$list['id']] = $list['catname'];
}
// print_r($cat);die;
}
return $cat;
}
contoller
$cats = Catmodel::getCatlist();
//print_r($cats);die;
return $this->render('index', [
'model' => $model,
'cats' => $cats,
]);
view
<?=Html::activeDropDownList($model,'selects',$cats,['class'=>'form-control'])?>
<?=Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?=Html::resetButton('重置', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>