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

Source: Internet
Author: User

This article illustrates the method of implementing the search paging function with the YII framework combined with Sphinx,ajax. Share to everyone for your reference, specific as follows:

Effect Chart:

Controller:

<?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 {//Display search page Public Function Actionindex () {//Accept 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);
    $CL-> setconnecttimeout (3);
    $CL-> Setarrayresult (true);
    if ($sou) {//only search conditions $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 (); Keyword Red foreach ($models as $k => $v) {$models [$k] [' Goods_name ']=str_replace (' $sou ', ' <font color= ' red ' &G t; $sou </font> ", $v [' goods_name ']);//replace keyword with red font//display list, assign data return $this->render (' Index ', ['
   Res ' => $models, ' pages ' => $pages, ' sou ' => $sou, ' p1 ' => $p 1, ' P2 ' => $p 2]);

 }}?>

View Layer:

<?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> <d IV id= "LIST" > Product Name: <input type= "text" name= "sou" value= "<?php echo $sou?>" > Price range: <input type= "
  Text "Name=" P1 "value=" <?php echo $p 1?> ">---<input type=" text "name=" P2 "value=" <?php echo $p 2?> "> <input type= "Submit" value= "search" > <table border= "1" style= "width:500px;" > <tr> <th>ID</th> <th> product name </th> <th> Product price </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> <!--pagination--> <?= linkpager::widget ([' Pagination ' => $pages]) ?> </div> </center> <?php activeform::end ()?> <!--display--> <?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 who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the 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.