标签:
控制器层 <?php namespace 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 { //安装coreseek,进行测试 public function actionList() { //获取搜索值 $key_word = Yii::$app->request->get(‘key‘); //memcache缓存 $mem=new Memcache(); if($key_word) { //key值 $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);die; if($data==‘‘) { $data=array(); } if(!in_array($key_word,$data)) { $data[]=$key_word; $mem->set(‘serach‘,$data,3600); } } } else { $mem->set($serach,‘1‘,3600); } } //分页 $pager = new Pagination(); $pager->defaultPageSize = 3; $pager->totalCount = 100; //使用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);die; $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]); //从memcache取值 $info=$mem->get(‘serach‘); if(Yii::$app->request->isAjax){ Yii::$app->response->format=\yii\web\Pesponse::FORMAT_JSON; return [‘rows‘=>$rows,‘linkPager‘=>$linkPager,‘info‘=>$info]; } //print_r($res);die; return $this->render(‘index‘,[‘linkPager‘=>$linkPager, ‘rows‘=>$rows,‘info‘=>$info]); } //批量删除 public function actionDel() { $str=$_GET[‘ids‘]; if($str == ‘‘) { echo ‘请选择对象‘; } $a=Yii::$app->db; $res=$a->createCommand("delete from zhan where id in ($str)")->execute(); if($res) { echo ‘1‘; } else { echo ‘-1‘; } } //隐藏 public function actionPcang() { $str=$_GET[‘ids‘]; if($str == ‘‘) { echo ‘请选择对象‘; } $a=Yii::$app->db; $res=$a->createCommand("update zhan set status=1 where id in ($str)")->execute(); if($res) { echo ‘1‘; } else { echo ‘-1‘; } } } ?> 视图层 <?php use yii\helpers\Html; use yii\widgets\ActiveForm; use yii\widgets\LinkPager; $form = ActiveForm::begin([ ‘method‘ => ‘get‘, ‘action‘ => ‘index.php?r=sphinx/list‘ ]) ?> 关键字:<input type="text" name="key"> <?= Html::submitButton(‘搜索‘, [‘class‘ => ‘btn btn-primary‘]) ?> <input type="checkbox" name=‘ids[]‘ class=‘yin‘ onclick="pcang()" >显示隐藏 <?php ActiveForm::end() ?> <a href="javascript:void(0)" onclick="qx()">全选</a>|| <a href="javascript:void(0)" onclick="fx()">反选</a>|| <a href="javascript:void(0)" onclick="qbx()">全不选</a>|| <a href="javascript:void(0)" onclick="del()">删除</a> <!--遍历数组--> <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分页和关键字变红--> <?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; }); //全选 function qx() { var ids=$(".ids"); for(var i=0;i<ids.length;i++) { ids[i].checked=true; } } //全不选 function qbx() { var ids=$(‘.ids‘); for(var i=0;i<ids.length;i++) { ids[i].checked=false; } } //反选 function fx() { var ids=$(‘.ids‘); for(var i=0;i<ids.length;i++) { ids[i].checked=!ids[i].checked; } } //删除 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(‘请选择对象‘); } var url="index.php?r=sphinx/del"; var data={‘ids‘:ids}; $.get(url,data,function(msg) { //alert(msg); if(msg==-1) { alert(‘删除失败‘); } else { location.href="index.php?r=sphinx/list"; } }); } //批量隐藏 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(‘失败‘); } else { location.href=‘index.php?r=sphinx/list‘; } }); } <?php $this->endBlock();$this->registerJs($this->blocks[‘test2‘], \yii\web\View::POS_END);?>
sphinx ajax搜索分页,memcache缓存,批量删除
标签:
原文地址:http://www.cnblogs.com/jhy-ocean/p/5341885.html