簡體   English   中英

從控制器檢索數據並在視圖中顯示-Yii2-Basic-App

[英]Retrieve data from controllers and show in views - Yii2-Basic-App

在我的表格中,我有8-9個字段。 其中所有都是硬編碼的,除了其中一個應該來自數據庫。

UsersControllers.php (控制器)

public function actionRegister() 
{
    .
    .
    model = new Users();
    $modelUserType=UserType::find()->select(['id', 'type'])->where(['status' => 1])->all();
        return $this->render('register', [
            'model' => $model,
            'modelUserType'=>$modelUserType,
            'module' => $this->module,
      ]);
}

如果我做print_r($modelUserType);值就來了print_r($modelUserType);

但是,我不知道在單選按鈕視圖中顯示所有那些“ UserType”表值。

<?=
    $form->field($modelUserType, 'user_type')
    ->radioList(array('1' => 'An Individual', '2' => 'Firm')) // Here.
    ->label('Are You')
?>

register.php (查看)

.
.

<?= $form->field($model, 'password')->passwordInput()->label('Password') ?> 
<?= $form->field($model, 'confirm_password')->passwordInput()->label('Confirm Password') ?>

<?=
    $form->field($model, 'user_type')
    ->radioList(array('1' => 'An Individual', '2' => 'Firm'))
    ->label('Are You')
?>
<?= $form->field($model, 'company_name')->textInput() ?>
.
.

我沒有任何教程,或者找不到適合的教程。 幾乎不需要幫助。 請幫我。 謝謝。

public function actionRegister()
    {    
        $model = new Users();
        .
        .
        $modelUserType=UserType::find()->select(['id', 'type'])->where(['status' => 1])->asArray()->all();
    $type=[];
    foreach($modelUserType as $k=>$v){
                   if(is_array($v))$type[$v["id"]]=$v["type"];

    }


 return $this->render('register', [
          'model' => $model,
          'type'=>$type,
          'module' => $this->module,
    ]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM