基于Yii怎样写带分页的多条件搜索功能
Admin 2022-07-07 群英技术资讯 483 次浏览
本文实例讲述了Yii2.0框架实现带分页的多条件搜索功能。分享给大家供大家参考,具体如下:
方法一
在控制器中
public function actionShow(){ $where['title']=Yii::$app->request->get('title'); $where['content']=Yii::$app->request->get('content'); $query=new Query(); $query->from('votes'); // votes 是表名 if(!empty($where['title'])||!empty($where['content'])){ $query->andFilterWhere( ['like','title',$where['title']] )->orFilterWhere( ['like','content',$where['content']] ); } $users=$query->from('votes')->all(); $pages = new Pagination(['totalCount' =>$query->count(),'pageSize'=>'2']); $users = $query->offset($pages->offset)->limit($pages->limit)->all(); return $this->render('show',['data'=>$users,'where'=>$where,'pages'=>$pages]); }
在v层
<?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\helpers\Url; use yii\widgets\LinkPager; ?>
<?php $form=ActiveForm::begin([ 'action'=>Url::toRoute(['show']), 'method'=>'get', ]); echo '姓名'," ",Html::input('text','title'); echo '简介'," ",Html::input('text','content'); echo Html::submitButton('提交'); ActiveForm::end(); echo "<br/>"; echo "<br/>"; ?>
显示在v层的分页
<?php echo LinkPager::widget([ 'pagination'=>$pages, 'nextPageLabel'=>'下一页', 'firstPageLabel'=>'首页' ]) ?>
方法二(不带分页 是另外一种方法)
public function actionShow(){ $titles=Yii::$app->request->post('title'); $content=Yii::$app->request->post('content'); $where=1; if($titles!=""){ $where.=" and title like '%$titles%'"; } if($content!=""){ $where.=" and content like '%$content%'"; } $sql="select * from votes where $where"; $users=Yii::$app->db->createCommand($sql)->query(); return $this->render('show',['data'=>$users]); }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
一些朋友有遇到laravel访问public目录报错的情况,不知道怎样解决?其实解决这个问题并不难,接下来我们就来看laravel访问public报错的原因及解决方法,需要的朋友可以了解看看。
今天小编就为大家分享一篇tp5 实现列表数据根据状态排序,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
下面由Laravel教程栏目给大家介绍关于Laravel事件系统的运行原理,希望对大家有所帮助!
laravel定义路由的方法:1、利用get方式,语法为“Route::get(路径, function () {return 返回值;});”;2、利用post方式,语法为“Route::post(路径, 匿名方法);”。
如何用php生成html文件?小编觉得php生成HTML文件的类方法比较有意思,因此分享给大家参考参考,感兴趣的朋友可以了解一下,下面我们一起来看看php生成HTML文件的实现。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008