aiwo3336 2017-05-23 15:57:05 3107次浏览 0条评论 0 1 0
<?php

namespace frontend\models;
use yii\db\ActiveRecord;


class Zhoukao1 extends  ActiveRecord {
    public static function tableName(){
        return 'zhoukao1';
    }
    public  function  rules(){
        return[

        ];
    }
    public  function  attributeLabels(){
        return [
            'username'=>'用户名'
        ];
    }
}

控制器层

<?php

namespace frontend\controllers;
use yii\web\Controller;
use frontend\models\Zhoukao1;
use yii;
use db;


class YueController extends  Controller{
    public function actionIndex(){
        $model=new Zhoukao1();
        return $this->render('index',['model'=>$model]);
    }
    public function actionInsert(){
       $data=yii::$app->request->post('Zhoukao1');
        $aa=$data['username'];
        $model=new Zhoukao1();
        $model->username=$aa;
      $aa=  $model->save();
        if ($aa) {
            return    $this->redirect(['yue/select']);
        } else {
            echo "添加失败";
        }
    }
    public  function  actionSelect(){
        //$data = Zhoukao1::find()->where(['id'=>1])->asArray()->one();
        $data = Zhoukao1::find()->asArray()->all();
        return $this->render('show',['data'=>$data]);
    }
    public  function  actionDelete(){
      $id=  yii::$app->request->get('id');
     $exam = Zhoukao1::findOne($id);
     $aa=  $exam->delete();
        if ($aa) {
            return    $this->redirect(['yue/select']);
        } else {
            echo "删除失败";
        }
    }
    public  function  actionUpda(){
        $model=new Zhoukao1();
        $id=  yii::$app->request->get('id');
        $da = Zhoukao1::find()->where(['id'=>$id])->asArray()->one();
        $data=$da['username'];
        $ids=$da['id'];
        return $this->render('moren',['data'=>$data,'model'=>$model,'ids'=>$ids]);
    }
    public  function  actionUpdate()
    {
        $data=yii::$app->request->post('Zhoukao1');

        $exam = Zhoukao1::findOne($data['id']);
        $exam->username = $data['username'];
        $aa = $exam->save();
        if ($aa) {
            return    $this->redirect(['yue/select']);
        } else {
            echo "修改失败";
        }
    }
}

view层

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'action'=>'?r=yue/update',
    'method'=>'post'
]) ?>

<?php $model->username = $data; ?>
<?= Html::activeHiddenInput($model,'id',array('value'=> $ids)) ?>
<?= $form->field($model, 'username')->textInput(['style'=>'width:400px']) ?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('提交', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>
觉得很赞
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册