效果图:

控制器:

<?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);
        }

//设置价格(注意:创建索引时,价格属性定义为int)
        if($p1&&$p2)
        {
        $cl->SetFilterRange('price',$p1,$p2);
        }

//搜索查询关键字    
        $res = $cl->Query($sou,"mysql_goods");

//ajax分页
        $model=new Goods();
        foreach ($res['matches'] as $key => $val)
        {
          $ids[] = $val['id'];
        }
        //查询条件数据
        $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();
        //关键字变红
        foreach($models as $k=>$v)
        {
           $models[$k]['goods_name']=str_replace("$sou","<font color='red'>$sou</font>",$v['goods_name']);//将关键字替换成红色字体
        }

//显示列表,分配数据
        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?>">
   价格区间:
   <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>商品价格</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)
?>

yii中sphinx,Ajax搜索分页的更多相关文章

  1. yii2 sphinx Ajax搜索分页 关键词的缓存

    控制器层 <?php namespace frontend\controllers; use Yii; use yii\web\Controller; //use frontend\models ...

  2. Yii 框架ajax搜索分页

    要想实现ajax搜索分页 其实很简单 第一步:在 Yii 框架自带的搜索和分页正常运行的情况下,在视图层

  3. yii中sphinx索引配置解析

    #MySQL数据源配置,详情请查看:http://www.coreseek.cn/products-install/mysql/#请先将var/test/documents.sql导入数据库,并配置好 ...

  4. 在yii中使用分页

    yii中使用分页很方便,如下两种方法: 在控制器中: 1. $criteria = new CDbCriteria(); //new cdbcriteria数据库$criteria->id = ...

  5. Javascript and AJAX with Yii(在yii 中使用 javascript 和ajax)

    英文原文:http://www.yiiframework.com/wiki/394/javascript-and-ajax-with-yii /*** http://www.yiiframework. ...

  6. thinkphp中的ajax分页

    thinkphp中用ajax分页和普通的ajax分页的区别在于处理位置的不同,thinkphp是在控制器的方法中处理ajax传的值,然后返回数据.下面是一个点击事件触发后,显示的内容用ajax分页. ...

  7. 2018.2.28 PHP中使用jQuery+Ajax实现分页查询多功能如何操作

    PHP中使用jQuery+Ajax实现分页查询多功能如何操作 1.首先做主页Ajax_pag.php 代码如下 <!DOCTYPE html> <html> <head& ...

  8. PHP中使用jQuery+Ajax实现分页查询多功能操作

    1.首先做主页面Ajax_pag.php 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UT ...

  9. ecshop后台通过ajax搜索原理

    ecshop的搜索其实是功能十分强大的,但是ecshop搜索功能前台和后台还不大一样,前台主要是通过get方式,提交的url进行分页,而在ecshop的后台,则是接受表单的搜索条件,然后通过js发布到 ...

随机推荐

  1. C# Unicode编码与解码方法

    public static class ExtentMethod { public static string ToUnicodeString(this string str) { StringBui ...

  2. java NIO ;mvn

    http://ifeve.com/java-nio-scattergather/ mvn introduction(install,conf, proxy,plugin,samples) http:/ ...

  3. Mongo中的数组操作

    当前mongo中有这么一条数据 book是一个数组,在他后面添加一条数据 { "_id" : ObjectId("5721f504d1f70435632b5ce7&quo ...

  4. jQuery插件之Wookmark瀑布流

    使用方法: 1.下载wookmark.js 2.构建html <div class="wrapper"> <div id="con1_1"&g ...

  5. 关于带透明度的灰度层的show、hide

    原理图如下:[需要注意的是,灰度View与中间的小View是并列的关系,否则,带透明度的灰度图就会影响小View的透明度] - (void)show{ UIWindow *win = [[UIAppl ...

  6. Session服务器配置指南与使用经验

    一.摘要 所有Web程序都会使用Session保存数据. 使用独立的Session服务器可以解决负载均衡场景中的Session共享问题.本文介绍.NET平台下建立Session服务器的几种办法, 并介 ...

  7. iOS WIFI

    一.公共WIFI综述 现在很多公司都在做免费WIFI,车站.公交.地铁.餐厅,只要是人员密集流动的地方就有WIFI,免费WIFI从最初的网页认证方式也逐渐向客户端认证方式偏移.本文主要讨论iOS认证上 ...

  8. ZOOKEEPER解惑

    现在网上关于ZooKeeper的文章很多,有介绍Leader选举算法的,有介绍ZooKeeper Server内部原理的,还有介绍ZooKeeper Client的.本文不打算再写类似的内容,而专注与 ...

  9. ios runtime swizzle

    ios runtime swizzle @implementation NSObject(Extension) + (void)swizzleClassMethod:(Class)class orig ...

  10. DAC重置max server memory

    15:44 2014-01-24 08R2,一次通过GUI更改'最大服务器内存(MB)'为16MB,errorlog显示信息如下 :: . Run the RECONFIGURE statement ...