Example of Yii 2.0 framework implementing multi condition search function with paging
- 2021-11-29 06:17:14
- OfStack
In this paper, an example is given to describe the implementation of multi-condition search function with paging in Yii 2.0 framework. Share it for your reference, as follows:
Method 1
In the controller
public function actionShow(){
$where['title']=Yii::$app->request->get('title');
$where['content']=Yii::$app->request->get('content');
$query=new Query();
$query->from('votes');
// votes Is a table name
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]);
}
At the v layer
<?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 ' Name '," ",Html::input('text','title');
echo ' Brief introduction '," ",Html::input('text','content');
echo Html::submitButton(' Submit ');
ActiveForm::end();
echo "<br/>";
echo "<br/>";
?>
Pages displayed at v layer
<?php
echo LinkPager::widget([
'pagination'=>$pages,
'nextPageLabel'=>' Under 1 Page ',
'firstPageLabel'=>' Home page '
])
?>
Method 2 (without paging is another method)
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]);
}
More readers interested in Yii can check the topics of this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Basic Tutorial of Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
I hope this article is helpful to PHP programming based on Yii framework.