yii DAO yii框架增删改查 分页 验证码 框架自带表单

<?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,
            ],
        ];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值