Yii框架结合sphinx Ajax如何实现搜索分页功能
导读:本文共1975.5字符,通常情况下阅读需要7分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 具体如下:效果图:控制器:<?phpnamespacebackend\controllers;useYii;useyii\web\Controller;useyii\data\Pagination;useSphinxClient;useyii\db\Query;useyii\widgets\LinkPager;usebackend\mode... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。具体如下:
效果图:
控制器:
<?phpnamespacebackend\controllers;useYii;useyii\web\Controller;useyii\data\Pagination;useSphinxClient;useyii\db\Query;useyii\widgets\LinkPager;usebackend\models\Goods;classSouControllerextendsController{//显示搜索页面publicfunctionactionIndex(){//接受搜索值$sou=Yii::$app->request->get('sou');$p1=Yii::$app->request->get('p1');$p2=Yii::$app->request->get('p2');//echo$sou.$p1.$p2;die;//sphinx搜索$cl=newSphinxClient();$cl->SetServer('127.0.0.1',9312);$cl->SetConnectTimeout(3);$cl->SetArrayResult(true);if($sou){//只搜索条件$cl->SetMatchMode(SPH_MATCH_ANY);}else{//全局扫描$cl->SetMatchMode(SPH_MATCH_FULLSCAN);}//设置价格(注意:创建索引时,价格属性定义为int)if($p1&&$p2){$cl->SetFilterRange('price',$p1,$p2);}//搜索查询关键字$res=$cl->Query($sou,"mysql_goods");//ajax分页$model=newGoods();foreach($res['matches']as$key=>$val){$ids[]=$val['id'];}//查询条件数据$query=$model->find()->where(['id'=>$ids]);$countQuery=clone$query;$pages=newPagination(['totalCount'=>$countQuery->count(),'defaultPageSize'=>3]);//分页$models=$query->offset($pages->offset)->limit($pages->limit)->all();//关键字变红foreach($modelsas$k=>$v){$models[$k]['goods_name']=str_replace("$sou","<fontcolor='red'>$sou</font>",$v['goods_name']);//将关键字替换成红色字体}//显示列表,分配数据return$this->render('index',['res'=>$models,'pages'=>$pages,'sou'=>$sou,'p1'=>$p1,'p2'=>$p2]);}}?>
视图层:
<?phpuseyii\helpers\Html;useyii\widgets\ActiveForm;useyii\widgets\LinkPager;$form=ActiveForm::begin(['action'=>'index.php?r=sou/index','method'=>'get'])?><center><divid="list">商品名称:<inputtype="text"name="sou"value="<?phpecho$sou?>">价格区间:<inputtype="text"name="p1"value="<?phpecho$p1?>">---<inputtype="text"name="p2"value="<?phpecho$p2?>"><inputtype="submit"value="搜索"><tableborder="1"><tr><th>ID</th><th>商品名称</th><th>商品价格</th></tr><?phpforeach($resas$key=>$v){?><tr><td><?phpecho$v['id'];?></td><td><?phpecho$v['goods_name'];?></td><td><?phpecho$v['price'];?></td></tr><?php}?></table><!--分页--><?=LinkPager::widget(['pagination'=>$pages])?></div></center><?phpActiveForm::end()?><!--显示--><?php$this->beginBlock('test2')?>$(document).on('click','.paginationa',function(e){//阻止page显示,看地址e.preventDefault();varhref=$(this).attr('href');$.post(href,function(msg){$('#list').html(msg);})});<?php$this->endBlock();$this->registerJs($this->blocks['test2'],yii\web\View::POS_END)?>
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
Yii框架结合sphinx Ajax如何实现搜索分页功能的详细内容,希望对您有所帮助,信息来源于网络。