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:---
| ID |
Product Name |
Product 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, 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)?>