YII框架的多条件查询

本文介绍了如何在YII框架中进行多条件查询并结合分页展示数据。通过`SearchController`中的`actionShow`方法,根据GET参数`mold`和`grade`动态构建查询条件,并利用`Pagination`进行分页。在视图层,使用`ActiveForm`创建表单元素,展示查询结果,并用`LinkPager`生成分页链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控制器:

<?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;
/**
 * Site controller
 */
class SearchController extends Controller
{
    //展示数据
    public function actionShow()
    {
        $where = Yii::$app->request->get();
        $query = new \yii\db\Query();
        $query->from('search');
        //判断where条件
        if(!empty($where['mold'])){
            $query->andWhere(['mold'=>$where['mold']]);
        }
        if(!empty($where['grade'])){
            $query->andWhere(['grade'=>$where['grade']]);
        }
        $data = $query->from('search')->all();
        // return $this->render('show',['data'=>$data]);
        $pages = new  Pagination([
            //'totalCount' => $countQuery->count(),
            'totalCount' => $query->count(),
            'pageSize'   => '3'  //每页显示条数
        ]);
                     $arr = $query->offset($pages->offset)->limit($pages->limit)->all();
                     return $this->render("show",['data'=>$data,'where'=>$where,'pages'=>$pages]);
    }
}



视图层:

<?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>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>


<?php
 $form = ActiveForm::begin([
            'action'=>Url::toRoute(['search/show']),
            'method'=>'get',
    ]);             
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>


我试了一下分页,出现了问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值