Yii framework combines sphinx and Ajax to implement search paging function example_php example

WBOY
Release: 2023-03-03 06:24:01
Original
1221 people have browsed it

The example in this article describes how the Yii framework combines sphinx and Ajax to implement the search paging function. Share it with everyone for your reference, the details are as follows:

Rendering:

Controller:

<&#63;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
    ]);
   }
}
&#63;>

Copy after login

View layer:

<&#63;php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;
$form = ActiveForm::begin([
  'action' => 'index.php&#63;r=sou/index',
  'method' => 'get'
]) &#63;>
<center>
<div id="list">
  商品名称:
  <input type="text" name="sou" value="<&#63;php echo $sou&#63;>">
  价格区间:
  <input type="text" name="p1" value="<&#63;php echo $p1&#63;>">---<input type="text" name="p2" value="<&#63;php echo $p2&#63;>">
  <input type="submit" value="搜索">
  <table border="1" style="width:500px;">
    <tr>
      <th>ID</th>
      <th>商品名称</th>
      <th>商品价格</th>
    </tr>
    <&#63;php foreach($res as $key=>$v){&#63;>
    <tr>
      <td><&#63;php echo $v['id'];&#63;></td>
      <td><&#63;php echo $v['goods_name'];&#63;></td>
      <td><&#63;php echo $v['price'];&#63;></td>
    </tr>
    <&#63;php }&#63;>
 </table>
<!--分页-->
<&#63;= LinkPager::widget(['pagination' => $pages]) &#63;>
</div>
</center>
<&#63;php ActiveForm::end() &#63;>
<!--显示-->
<&#63;php $this->beginBlock('test2') &#63;>
  $(document).on('click', '.pagination a', function(e)
  {
    //阻止page显示,看地址
    e.preventDefault();
    var href = $(this).attr('href');
    $.post(href,function(msg){
      $('#list').html(msg);
    })
  });
<&#63;php $this->endBlock();
$this->registerJs($this->blocks['test2'] , yii\web\View::POS_END)
&#63;>

Copy after login

Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string (string) usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will help you design PHP programs based on the Yii framework.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!