Yii2.0框架實現帶分頁的多條件搜索功能示例
本文實例講述了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]); }
更多關于Yii相關內容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
相關文章
PHP模板引擎Smarty內建函數foreach,foreachelse用法分析
這篇文章主要介紹了PHP模板引擎Smarty內建函數foreach,foreachelse用法,結合實例形式分析了foreach,foreachelse的功能與具體使用技巧,需要的朋友可以參考下2016-04-04PHP中的gzcompress、gzdeflate、gzencode函數詳解
這篇文章主要介紹了PHP中的gzcompress、gzdeflate、gzencode函數詳解,本文深入分析了它們的相同點和不同點,需要的朋友可以參考下2014-07-07PHPMailer發(fā)送HTML內容、帶附件的郵件實例
這篇文章主要介紹了PHPMailer發(fā)送HTML內容、帶附件的郵件實例,發(fā)送的內容包含圖片和文字,附件則發(fā)送的一個EXCEL表,需要的朋友可以參考下2014-07-07thinkphp5.1框架中容器(Container)和門面(Facade)的實現方法分析
這篇文章主要介紹了thinkphp5.1框架中容器(Container)和門面(Facade)的實現方法,結合實例形式分析了thinkPHP5.1框架中容器與門面的定義、實現方法及相關操作注意事項,需要的朋友可以參考下2019-08-08