Yii框架结合sphinx Ajax如何实现搜索分页功能(ajax,sphinx,yii框架,开发技术)

时间:2024-04-30 05:37:08 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

具体如下:

效果图:

Yii框架结合sphinx Ajax如何实现搜索分页功能

控制器:

<?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如何实现搜索分页功能的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:set()与get()拦截器如何在PHP中使用下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18