This paper describes the method of implementing the search paging function by the YII framework combined with Sphinx,ajax. Share to everyone for your reference, as follows:
:
Controller:
<?phpnamespace 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{//Show Search page Public Function Actionindex () {//accepts the search value $sou =yii:: $app->request->get (' sou '); $p 1=yii:: $app->request->get (' P1 '); $p 2=yii:: $app->request->get (' P2 '); Echo $sou. $p 1. $p 2;die; Sphinx Search $CL = new sphinxclient (); $CL-Setserver (' 127.0.0.1 ', 9312); Setconnecttimeout, $CL (3); $CL-Setarrayresult (true); if ($sou) {//Search condition $CL-Setmatchmode (Sph_match_any); } else {//Global scan $CL-Setmatchmode (Sph_match_fullscan); }//Set price (note: When creating an index, the price attribute is defined as int) if ($p 1&& $p 2) {$cl->setfilterrange (' Prices ', $p 1, $p 2); }//Search query keyword $res = $cl->query ($sou, "mysql_goods"); Ajax Paging $model =new Goods (); foreach ($res [' matches '] as $key = $val){$ids [] = $val [' id ']; }//Query condition Data $query = $model->find ()->where ([' id ' = = $ids]); $countQuery = Clone $query; $pages = new Pagination ([' totalcount ' = = $countQuery->count (), ' defaultpagesize ' =>3]); Paging $models = $query->offset ($pages->offset)->limit ($pages->limit)->all (); The keyword turns red foreach ($models as $k + $v) {$models [$k] [' Goods_name ']=str_replace ("$sou", "$sou", $v [' goods_name ']); /replace keyword with red font}//display list, assign data return $this->render (' Index ', [' res ' + $models, ' pages ' + $pag Es, ' sou ' and $sou, ' p1 ' and ' = $p 1, ' P2 ' + $p 2]); }}?>
View Layer:
<?phpuse yii\helpers\html;use yii\widgets\activeform;use yii\widgets\linkpager; $form = ActiveForm::begin ([' Action ' = ' index.php?r=sou/index ', ' method ' = ' get '])?>
Product Name:
Price range:
---
ID |
Product Name |
Commodity price |
<?php foreach ($res as $key = $v) {?>
<?php echo $v [' id '];? > |
<?php echo $v [' goods_name '];? > |
<?php echo $v [' price '];? > |
<?php}?>
<?= linkpager::widget ([' pagination ' + $pages])?>
<?php activeform::end ()?>
<?php $this->beginblock (' test2 ')?> $ (document). On (' click ', '. Pagination a ', function (e) {//block page display, see address 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::P os_end)?>
For more information on YII related content readers can view this site topic: "YII framework Introduction and common skills Summary", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "
It is hoped that this article is helpful to the PHP program design based on YII framework.