Yii框架結(jié)合sphinxAjax如何實(shí)現(xiàn)搜索分頁(yè)功能-創(chuàng)新互聯(lián)

這篇文章主要介紹Yii框架結(jié)合sphinx Ajax如何實(shí)現(xiàn)搜索分頁(yè)功能,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)自2013年起,先為陸豐等服務(wù)建站,陸豐等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為陸豐企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

具體如下:

效果圖:

Yii框架結(jié)合sphinx Ajax如何實(shí)現(xiàn)搜索分頁(yè)功能

控制器:

<?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
{
  //顯示搜索頁(yè)面
  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分頁(yè)
    $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]);
    //分頁(yè)
    $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" >
    <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>
<!--分頁(yè)-->
<?= 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)
?>

以上是“Yii框架結(jié)合sphinx Ajax如何實(shí)現(xiàn)搜索分頁(yè)功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁(yè)名稱:Yii框架結(jié)合sphinxAjax如何實(shí)現(xiàn)搜索分頁(yè)功能-創(chuàng)新互聯(lián)
文章來源:http://bm7419.com/article24/cdijce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站