Sphinx Ajax Search Paging, memcache cache, bulk Delete

Source: Internet
Author: User
Tags php foreach yii

Controller layer <?phpnamespace frontend\controllers;use yii;use yii\web\controller;//use frontend\models\zhan;use yii\data\ Pagination;use sphinxclient;use yii\db\query;use yii\widgets\linkpager;use yii\caching\memcache;class Sphinxcontroller extends controller{//install Coreseek, test public Function actionlist () {//Get Search      Value $key _word = Yii:: $app->request->get (' key ');      Memcache Cache $mem =new Memcache ();    if ($key _word) {//key value $serach =md5 ($key _word);    $num = $mem->get ($serach);                if ($num) {if ($num <5) {$mem->set ($serach, $num +1,3600);                    } else {$data = $mem->get (' Serach ');                Var_dump ($data);d ie;                if ($data = = ") {$data =array ();                 } if (!in_array ($key _word, $data)) {$data []= $key _word;      $mem->set (' Serach ', $data, 3600);    }}}} else {$mem->set ($serach, ' 1 ', 3600);            }//Paging $pager = new pagination (), $pager->defaultpagesize = 3; $pager->totalcount = 100; Use SHINX$CL = new Sphinxclient (), $CL, Setserver (' 127.0.0.1 ', 9312), $cl setconnecttimeout (3); $cl Setarrayresult (True), $cl-Setmatchmode (Sph_match_any);//$cl->setfilter (' cat_id ', [0]); $CL->setlimits ($ Pager->getoffset (), $pager->getlimit ()), $res = $cl->query ($key _word, "*"),//print_r ($res);d ie; $pager TotalCount = $res [' Total '], $rows = Array (), if ($res [' Total ']>0) {$ids = [];foreach ($res [' matches '] as $key = + $val) { $ids [] = $val [' id '];} $model = new Query (), $rows = $model->from (' Zhan ')->where ([' In ', ' id ', $ids])->andwhere ([' Status ' =>0])- >all ();} $linkPager =linkpager::widget ([' pagination ' = $pager]);//$info= $mem->get (' Serach ') from Memcache; if (yii::$ App->request->isajax) {Yii:: $app->rEsponse->format=\yii\web\pesponse::format_json;return [' rows ' = = $rows, ' Linkpager ' + $linkPager, ' info ' = > $info];} Print_r ($res);d ie;return $this->render (' Index ', [' Linkpager ' + $linkPager, ' rows ' and ' = ' $rows, ' info ' =>$ Info]);} Bulk Delete public Function Actiondel () {$str =$_get[' IDs '];if ($str = = ") {echo ' Please select object ';}    $a =yii:: $app->db; $res = $a->createcommand ("delete from Zhan where ID in ($STR)")->execute (); if ($res) {echo ' 1 ';}    else {echo '-1 '; }}//Hide Public Function Actionpcang () {$str =$_get[' IDs '];if ($str = = ") {echo ' Please select object ';} $a =yii:: $app->db; $res = $a->createcommand ("Update Zhan set Status=1 where ID in ($STR)")->execute (); if ($res) {    echo ' 1 ';}    else {echo '-1 '; }}}?> View Layer <?phpuse yii\helpers\html;use yii\widgets\activeform;use yii\widgets\linkpager; $form = ActiveForm:: Begin ([' method ' = ' get ', ' action ' = ' index.php?r=sphinx/list '])?> keyword: <input type= "text" Name= " Key "> <?= Html::submitbutton (' Search ', [' class ' => ' Btn btn-primary '])?> <input type= "checkbox" Name= ' ids[] ' class= ' Yin ' onclick= "Pcang ()" > Show hidden <?php Acti Veform::end ()? ><a href= "javascript:void (0)" onclick= "QX ()" > select All </a>| | <a href= "javascript:void (0)" onclick= "FX ()" > Anti-selection </a>| | <a href= "javascript:void (0)" onclick= "QBX ()" > all without </a>| | <a href= "javascript:void (0)" onclick= "Del ()" > Delete </a><!--traverse array--><div id= "Rows" ><p> <?php if ($info) {foreach ($info as $key = = $v) {><font color= "red" ><?php echo $v;?  ></font> <?php}}?> </p><?php foreach ($rows as $key = $v) {?> <input type= "checkbox" Name= "ids[]" class= "IDs" value= "<?php echo $v [' id ']?> ' > <?php echo $v [' title ']. ' <br/> '? ><?php}?></div><div id= "pager" ><?= $linkPager?></div><!-- Ajax Paging and keywords turn red--><?php $this->beginblock (' test2 ')? >$ (document). On (' click ', '. Pagination a ', function () { var href=$ (This). attr (' href '); $.getjson (href,function (data) {var rows=data.rows;       var html= ';       for (Var i=0;i<rows.length;i++) {html+= ' <p> ' + rows[i].name + ' </p> ';       } $ (' #rows '). HTML (HTML); $ (' #pager '). HTML (data.pagination);}) return false;});   /Select All function qx () {var ids=$ (". IDs");    for (Var i=0;i<ids.length;i++) {ids[i].checked=true;   }}//all do not select function Qbx () {var ids=$ ('. IDs ');   for (Var i=0;i<ids.length;i++) {ids[i].checked=false;   }}//Counter-select function fx () {var ids=$ ('. IDs ');   for (Var i=0;i<ids.length;i++) {ids[i].checked=!ids[i].checked;    }}//Delete Function del () {var ids=$ ('. IDs ');        var str= ';      for (Var i=0;i<ids.length;i++) {if (ids[i].checked = = true) {str+= ', ' +ids[i].value;    }} ids=str.substr (1);    if (ids = = ") {alert (' Please select object ');    } var url= "Index.php?r=sphinx/del";    var data={' IDs ': IDs};       $.get (Url,data,function (msg) {//alert (msg); if (msg==-1) {alert (' delete failed ');        } else {location.href= "index.php?r=sphinx/list"; }    });}    Bulk Hide Function Pcang () {var ids=$ ('. IDs ');    var str= ';      for (Var i=0;i<ids.length;i++) {if (ids[i].checked==true) {str+= ', ' +ids[i].value;    }} ids=str.substr (1);    alert (IDs);    var data={' IDs ': IDs};    var url= "Index.php?r=sphinx/pcang";      $.get (Url,data,function (msg) {//alert (msg);      if (msg==-1) {alert (' failed ');      } else {location.href= ' index.php?r=sphinx/list '; }    });} <?php $this->endblock (); $this->registerjs ($this->blocks[' test2 '], \yii\web\view::P os_end);? >

Sphinx Ajax Search Paging, memcache cache, bulk Delete

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.