Yii Framework combined with sphinx and Ajax implemented the search paging function example _ php instance

Source: Internet
Author: User
This article mainly introduces the Yii Framework, combined with sphinx and Ajax, to implement the search paging function. combined with the instance form, it analyzes the paging display effect of search results using sphinx and Ajax in the Yii Framework, for more information about how to use the Yii Framework combined with sphinx and Ajax to implement the paging function, see the example below. We will share this with you for your reference. The details are 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 {// display the search page public function actionIndex () {// accept the search value $ sou = Yii :: $ app-> request-> get ('sou'); $ p1 = Yii: $ app-> request-> get ('p1'); $ p2 = Yii :: $ app-> request-> get ('P2'); // echo $ sou. $ p1. $ p2; die; // sphin X search $ cl = new SphinxClient (); $ cl-> SetServer ('2017. 0.0.1 ', 9312); $ cl-> SetConnectTimeout (3); $ cl-> SetArrayResult (true); if ($ sou) {// only search for the condition $ cl-> SetMatchMode (SPH_MATCH_ANY);} else {// Global Scan $ cl-> SetMatchMode (SPH_MATCH_FULLSCAN);} // set the price (note: when creating an index, the price attribute is defined as int) if ($ p1 & $ p2) {$ cl-> SetFilterRange ('price', $ p1, $ p2 );} // search for the Query keyword $ res = $ cl-> Query ($ sou, "mysql_goods"); // ajax paging $ model = new Goods (); f Oreach ($ 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]); // page $ models = $ query-> offset ($ pages-> offset)-> limit ($ pages-> limit)-> all (); // foreach ($ models as $ k => $ v) {$ models [$ k] ['goods _ name'] = str_replace ("$ sou ", "$ sou ", $ V ['goods _ name']); // replace the keyword with a red font} // Display List, assign data return $ this-> render ('index ', ['res' => $ models, 'Pages '=> $ pages, 'sou' => $ sou, 'p1' => $ p1, 'p2 '=> $ p2]) ;}}?>

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 Product 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, View address e. preventDefault (); var href = $ (this ). attr ('href '); $. post (href, function (msg) {messages ('{list'{.html (msg) ;})}); <? Php $ this-> endBlock (); $ this-> registerJs ($ this-> blocks ['test2'], yii \ web \ View: POS_END)?>

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.