<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$this->title="curd";
?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'user')->label("用户名:") ?>
<?= $form->field($model, 'pwd')->label("密码:") ?>
<?= $form->field($model,'verifyCode')->widget(yii\captcha\Captcha::className())->label("验证码:")?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php
use yii\helpers\Html;
use yii\widgets\LinkPager;
?>
<center>
<table border="1">
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
<?php foreach($data as $k=>$v){?>
<tr>
<td><?php echo $v['id'] ;?></td>
<td><?php echo $v['user'] ;?></td>
<td><?php echo $v['pwd'] ;?></td>
<td><a href="?r=curd/del&id=<?php echo $v['id']?>">删除</a>||<a href="?r=curd/find&id=<?php echo $v['id']?>">修改</a></td>
</tr>
<?php
}?>
</table>
<?= LinkPager::widget(['pagination' => $pagination]) ?>
</center>
models
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
class Zsgc extends ActiveRecord{
public $user,$pwd,$verifyCode;
public function rules(){
return[
[['user','pwd'],"required"],
['verifyCode','captcha'] //添加验证码规则
];
}
}
控制器
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
//use yii\web\Response;
use app\models\Zsgc;
use yii\data\Pagination;
class CurdController extends Controller{
//添加
public function actionIndex(){
//实例化表
$model=new Zsgc;
//接受数据并验证合法性
if ($model->load(Yii::$app->request->post()) && $model->validate())
{
//接收数据
$arr=Yii::$app->request->post();
//添加到数据库
$res = Yii::$app->db->createCommand()->insert('zsgc',$arr['Zsgc'])->execute();
if($res){
header('refresh:0,url=?r=curd/select');
}
}
else{
// 无论是初始化显示还是数据验证错误
return $this->render('add', ['model' => $model]);
}
}
//查询
public function actionSelect(){
$query = zsgc::find();
$pagination = new Pagination([
'defaultPageSize' => 5,
'totalCount' => $query->count(),
]);
$countries = $query->orderBy('user')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('show', [
'data' => $countries,
'pagination' => $pagination,
]);
// $command = Yii::$app->db->createCommand('SELECT * FROM zsgc');
// $posts = $command->queryAll();
// return $this->render('show', ['data' => $posts]);
}
//删除
public function actionDel(){
$id=Yii::$app->request->get('id');
$res = Yii::$app->db->createCommand()->delete('zsgc', "id = $id")->execute();
if($res){
header('refresh:0,url=?r=curd/select');
}
else{
header('refresh:0,url=?r=curd/select');
}
}
//查询一条
public function actionFind(){
$model=new Zsgc;
$id=Yii::$app->request->get('id');
$command = Yii::$app->db->createCommand("SELECT * FROM zsgc WHERE id=$id");
$post = $command->queryOne();
return $this->render('update', ['data' => $post,'model'=>$model]);
}
//修改
public function actionUpdate(){
$arr=Yii::$app->request->post();
$res = Yii::$app->db->createCommand()->update('zsgc',$arr)->execute();
if($res){
header('refresh:0,url=?r=curd/select');
}
else{
header('refresh:0,url=?r=curd/find');
}
}
//验证码
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
}