Yii搜索分页加验证码

//Controller

<?php
namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\db\ActionIndex;
use common\models\LoginForm;
use frontend\models\PasswordResetRequestForm;
use frontend\models\ResetPasswordForm;
use frontend\models\SignupForm;
use frontend\models\ContactForm;
use frontend\models\Search;
use yii\data\Pagination;
use frontend\models\Code;
/**
 * Site controller
 */
class SearchController extends Controller
{
    //用户登录
    public function actionLogin(){
        $ses = yii::$app->session;
        return $this->render('login');
    }

    //展示数据
    public function actionShow()
    {
        echo '</pre>';
        //登录处理
        $user  = yii::$app->request->post('username');
        $pwd  = yii::$app->request->post('password');
        //接值处理
        $sql = "select * from login where username='$user' and password='$pwd'";
        $msg=Yii::$app->db->createCommand($sql)->queryAll();
        // print_r($msg[0]);die;

        $session = \YII::$app->session;
        $session->open();
        $session->set('user',$msg);

        

         $where = Yii::$app->request->get();
         $query = new \yii\db\Query();
         $query->from('search');
         if(!empty($where['mold'])) {
        $query->andWhere(['mold'=>$where['mold']]);
        // $timer = date('H:i:s',strtotime("+1 minute");
          }
          if (!empty($where['grade'])) {
             $query->andWhere(['grade'=>$where['grade']]);
          }
        
         $arr = $query->from('search')->all();
         // var_dump($arr);
         //分页码
         $pages = new Pagination([
            //'totalCount' => $countQuery->count(),
            'totalCount' => $query->count(),
            'pageSize'   => 5  //每页显示条数
        ]);
         $arr = $query->offset($pages->offset)->limit($pages->limit)->all();

        return $this->render("show",['data'=>$arr,'where'=>$where,'pages'=>$pages,'msg'=>$msg]);
     }

     //搜索处理
     public function actionSearch_act(){
         $session = \YII::$app->session;
        $session->open();
        $user = $session->get('user');
        $name = $user[0]['username'];
        $data = Yii::$app->request->post();
        $str = implode(',', $data);
        //用户有没有搜索
        $sql = "select * from search_act where user = '$name'";
        $msg=Yii::$app->db->createCommand($sql)->queryOne();
        //如果搜索过并满足搜索条件,搜索次数+1
        if($msg){
            //如果时间在1分钟之内,并次数在四次之内
            if(time()-$msg['a_time']<=60 && $msg['times']<=4){
                //如果时间在五秒之外,搜索次数+1
                if(time()-$msg['a_time'] >=30){
                    $up_data = array(
                            'user'=>$name,
                            'searchcon'=>$str,
                            'a_time'=>time(),
                            'times'=>$msg['times']+1,
                                     );
                    $res = Yii::$app
                    ->db
                    ->createCommand()
                    ->update('search_act',$up_data,['id'=>$msg['id']])
                    ->execute();
                    // var_dump($res);
                }else{
                    echo '<script>alert("亲,五秒后在搜索");location.href="?r=search/show"</script>';
                    // return $this->redirect('?r=search/show');
                }
            }else{
                echo '<script>alert("请输入验证码");location.href="?r=search/index"</script>';
                // return $this->redirect('?r=search/index');
            }
            
        }else{
            $insert_data = array(
                    'user'=>$name,
                    'searchcon'=>$str,
                    'a_time'=>time(),
                    'times'=>1,
                             );
                    $result = Yii::$app
                    ->db
                    ->createCommand()
                    ->insert('search_act',$insert_data)
                    ->execute();
        }


     }

     //验证码
     public function actionCode(){
                return [
                    'error' => [
                        'class' => 'yii\web\ErrorAction',
                    ],
                    'captcha' => [
                        'class' => 'yii\captcha\CaptchaAction',
                        'maxLength'=>3,
                        'minLength'=>3,
                    ],
                ];
     }
    public function actionIndex(){
        //调用model
           $model = new Code();
          
     return $this->render('code',[
                'model'=>$model,
            ]);
        }
}


//View

//Login

<?php

/* @var $this yii\web\View */


use yii\helpers\Html;
use yii\base\Widget;
$this->title = '登录';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1>登录页面</h1>
    <form action="?r=search/show" method="post" class="form">
        <table class="table">
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="text" name="password"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="登录"></td>
            </tr>
        </table>
    </form>
</body>
</html>


//Show

<?php

/* @var $this yii\web\View */


use yii\helpers\Html;
use yii\helpers\Url;
use yii\base\Widget;
use yii\widgets\ActiveForm;
use \yii\widgets\LinkPager;

$this->title = '多条件查询';
?>
<h1><?= Html::encode($this->title) ?></h1>
<h2>欢迎登录</h2>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>


<?php
 $form = ActiveForm::begin([
            'action'=>Url::toRoute(['search/search_act']),
            'method'=>'post',
    ]);             
echo "公司类型:".Html::input('text','mold');
echo "公司等级:".Html::input('text','grade');
echo Html::submitButton('搜索',['class'=>'btn btn-primary']);

ActiveForm::end();
?>
    <table class="table">
        <tr>
            <th>ID</th>
            <th>公司名称</th>
            <th>公司类型</th>
            <th>金额</th>
            <th>公司等级</th>
            <th>还款方式</th>
            <!-- <th>操作</th> -->
        </tr>
        <?php foreach ($data as $key => $value): ?>
            <tr>
                <td><?php echo $value['id']?></td>
                <td><?php echo $value['company']?></td>
                <td><?php echo $value['mold']?></td>
                <td><?php echo $value['money']?></td>
                <td><?php echo $value['grade']?></td>
                <td><?php echo $value['mode']?></td>
                <!-- <td><a href="?r=search/show&id=<?php //echo $value['id']?>">删除</a></td> -->
            </tr>
        <?php endforeach ?>
    </table>
    <?php
    echo LinkPager::widget([
    'pagination'=>$pages,
    'nextPageLabel'=>"下一页",
    'firstPageLabel'=>"首页",
    ])
    ?>
</body>
</html>

//Code


<?php
use common\widgets;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
use yii\helpers\Url;
use yii\widgets\LinkPager;
use yii\base;
?>

<?php $form=ActiveForm::begin([
            'action'=>Url::toRoute(['show']),
            'method'=>'post',
         ]); ?>
 <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
            'template' => '<div class="row"><div class="col-lg-4">{image}</div><div class="col-lg-6">{input}</div></div>',
 ]) ?>
 <?php ActiveForm::end(); ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值