Yii framework combined with Sphinx,ajax to implement the search paging function Example _php instance

Source: Internet
Author: User
Tags php foreach smarty template yii
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:
   ---
     
 
     
 
   
 <?php foreach ($res as $key = $v) {?> 
   <?php}?> 
  
ID Product Name Commodity price
<?php echo $v [' id '];? > <?php echo $v [' goods_name '];? > <?php echo $v [' price '];? >
<?= 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.