Yii框架結(jié)合sphinx,Ajax實(shí)現(xiàn)搜索分頁功能示例
本文實(shí)例講述了Yii框架結(jié)合sphinx,Ajax實(shí)現(xiàn)搜索分頁功能的方法。分享給大家供大家參考,具體如下:
效果圖:

控制器:
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use SphinxClient;
use yii\db\Query;
use yii\widgets\LinkPager;
use backend\models\Goods;
class SouController extends Controller
{
//顯示搜索頁面
public function actionIndex()
{
//接受搜索值
$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 = new SphinxClient();
$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);
}
//設(shè)置價(jià)格(注意:創(chuàng)建索引時(shí),價(jià)格屬性定義為int)
if($p1&&$p2)
{
$cl->SetFilterRange('price',$p1,$p2);
}
//搜索查詢關(guān)鍵字
$res = $cl->Query($sou,"mysql_goods");
//ajax分頁
$model=new Goods();
foreach ($res['matches'] as $key => $val)
{
$ids[] = $val['id'];
}
//查詢條件數(shù)據(jù)
$query = $model->find()->where(['id'=>$ids]);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(),'defaultPageSize'=>3]);
//分頁
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
//關(guān)鍵字變紅
foreach($models as $k=>$v)
{
$models[$k]['goods_name']=str_replace("$sou","<font color='red'>$sou</font>",$v['goods_name']);//將關(guān)鍵字替換成紅色字體
}
//顯示列表,分配數(shù)據(jù)
return $this->render('index', [
'res' => $models,
'pages' => $pages,
'sou'=>$sou,
'p1'=>$p1,
'p2'=>$p2
]);
}
}
?>
視圖層:
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;
$form = ActiveForm::begin([
'action' => 'index.php?r=sou/index',
'method' => 'get'
]) ?>
<center>
<div id="list">
商品名稱:
<input type="text" name="sou" value="<?php echo $sou?>">
價(jià)格區(qū)間:
<input type="text" name="p1" value="<?php echo $p1?>">---<input type="text" name="p2" value="<?php echo $p2?>">
<input type="submit" value="搜索">
<table border="1" style="width:500px;">
<tr>
<th>ID</th>
<th>商品名稱</th>
<th>商品價(jià)格</th>
</tr>
<?php foreach($res as $key=>$v){?>
<tr>
<td><?php echo $v['id'];?></td>
<td><?php echo $v['goods_name'];?></td>
<td><?php echo $v['price'];?></td>
</tr>
<?php }?>
</table>
<!--分頁-->
<?= LinkPager::widget(['pagination' => $pages]) ?>
</div>
</center>
<?php ActiveForm::end() ?>
<!--顯示-->
<?php $this->beginBlock('test2') ?>
$(document).on('click', '.pagination a', function(e)
{
//阻止page顯示,看地址
e.preventDefault();
var href = $(this).attr('href');
$.post(href,function(msg){
$('#list').html(msg);
})
});
<?php $this->endBlock();
$this->registerJs($this->blocks['test2'] , yii\web\View::POS_END)
?>
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
docker-compose部署php項(xiàng)目實(shí)例詳解
在本篇文章里小編給大家整理了關(guān)于docker-compose部署php項(xiàng)目的相關(guān)實(shí)例以及代碼內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2019-07-07
thinkphp中AJAX返回ajaxReturn()方法分析
這篇文章主要介紹了thinkphp中AJAX返回ajaxReturn()方法,結(jié)合實(shí)例形式分析了thinkPHP中ajax操作的功能、數(shù)據(jù)返回格式以及ajaxReturn方法的簡單使用技巧,需要的朋友可以參考下2016-12-12
PHP中使用sleep函數(shù)實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例分享
這篇文章主要介紹了PHP中使用sleep函數(shù)實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例分享,本文給出了多個(gè)使用sleep的例子,本文還可以作為學(xué)習(xí)sleep函數(shù)的教程,需要的朋友可以參考下2014-08-08
thinkPHP5使用laypage分頁插件實(shí)現(xiàn)列表分頁功能
這篇文章主要為大家詳細(xì)介紹了thinkPHP5使用laypage分頁插件實(shí)現(xiàn)列表分頁功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

