Yii框架搜索分页

这篇博客介绍了如何在Yii框架下实现搜索和分页功能。在控制器中设置了搜索参数,视图部分展示了使用ActiveForm创建搜索表单,并通过LinkPager显示分页链接。用户可以输入姓名和ID进行搜索,搜索结果以表格形式展示,同时提供分页导航。
摘要由CSDN通过智能技术生成

controller: 

//搜索分页

    public function actionList()
    {
        $where = Yii::$app->request->get();
//        var_dump($where);
        $query = new Query();//刚写成 $query = new\yii\db\Query();报错,所以以后报错的时候多看看上面的use...
        $query->from("user");
        if (!empty($where['name'])){
        $query->andWhere(["name"=>$where['name']]);
        }

         if (!empty($where['id1'])&&$where['id1']!==""){
             $query->andWhere([">=","id",$where['id1']]);
         }

         if (!empty($where['id2'])&&$where['id2']!==""){
             $query->andWhere(["<=","id",$where['id2']]);
         }
         //$user = $query->all();//执行上面所有的语句
        //$count = count($user);
        /*分页*/
//        $pagination = new Pagination(['totalCount' => $count,'pageSize'=>2]);
        $pagination = new Pagination(['totalCount' => $query->count()]);
        $pagination->setPageSize(2);
        $user = $query->offset($pagination->offset)->limit($pagination->limit)->all();

        return $this->render("list",['user'=>$user,'where'=>$where,'pagination'=>$pagination]);

    }

 

 

view:<?php
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\helpers\Html;

?>
<?php
    $form = ActiveForm::begin([
            'action'=> Url::toRoute(['show/list']),
            'method'=>'get',
        ]);
    echo "姓名:",Html::input('text','name');
    echo "ID:",Html::input('text','id1');
    echo  "-",Html::input('text','id2');

    echo Html::submitButton("搜索");
    ActiveForm::end();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>搜索结果展示</title>
</head>
<body>
    <table border="1">
        <tr>
            <td>ID</td>
            <td>用户名</td>

        </tr>
        <?php foreach    ($user as $key => $value) { ?>
            <tr>
                <td><?php echo $value["id"];?></td>
                <td><?php echo $value["name"];?></php></td>

            </tr>
    <?php } ?>

    </table>
</body>
</html>

<?php
use yii\widgets\LinkPager;
    echo LinkPager::widget([
        'pagination'=>$pagination,
        'nextPageLabel'=>'下一页',
        'firstPageLabel'=>'首页',
        ]
    )?>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值