php实现多条件查找分页,Yii2.0框架实现带分页的多条件搜索功能示例
本文實(shí)例講述了Yii2.0框架實(shí)現(xiàn)帶分頁(yè)的多條件搜索功能。分享給大家供大家參考,具體如下:
方法一
在控制器中
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層
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;
use yii\widgets\LinkPager;
?>
$form=ActiveForm::begin([
'action'=>Url::toRoute(['show']),
'method'=>'get',
]);
echo '姓名'," ",Html::input('text','title');
echo '簡(jiǎn)介'," ",Html::input('text','content');
echo Html::submitButton('提交');
ActiveForm::end();
echo "
";
echo "
";
?>
顯示在v層的分頁(yè)
echo LinkPager::widget([
'pagination'=>$pages,
'nextPageLabel'=>'下一頁(yè)',
'firstPageLabel'=>'首頁(yè)'
])
?>
方法二(不帶分頁(yè)? 是另外一種方法)
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]);
}
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的php实现多条件查找分页,Yii2.0框架实现带分页的多条件搜索功能示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java初学者只要掌握了以下十大原则,可
- 下一篇: php数组o m n mn,O(m +