基于Yii怎样写带分页的多条件搜索功能
Admin 2022-07-07 群英技术资讯 538 次浏览
本文实例讲述了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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PHP中runkit扩展的使用有哪些知识点?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
php变量的特殊类型:1、资源(resource),是一种特殊的变量,保存在外部资源中。2、NULL,特殊的NULL表示一个变量没有值,NULL类型唯一可能的值是NULL。
在thinkphp中,n方法属于计数器方法,用于核心的查询、缓存统计的计数和统计,语法为“N('计数位置'[,'步进值'])”;n方法页面执行完毕后的统计结果不会带入下次统计。
本篇文章带大家来比较一下PHP5和PHP7的数组实现方式,看看它们之间的差异!
如何理解PHP与Java重载,两者什么差异?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008