控制器层 <?php namespace frontendcontrollers; use Yii; use yiiwebController; //use frontendmodelshan; use yiidataPagination; use SphinxClient; use yiidbQuery; use yiiwidgetsLinkPager; use yiicachingMemCache; class SphinxController extends Controller { //搜索 商品名称和价格 public function actionGoods(){ $key=Yii::$app->request->post('key',null); $mem = new Memcache; if($key != ''){ $sou = md5($key); $num = $mem->get($sou); if($num){ if($num < 5){ $num++; $mem->set($sou,$num,60*60); } else { //$mem->delete($sou); $data = $mem->get('sou'); if($data == ''){ $data = array(); } if(!in_array($key,$data)){ $data[] = $key; $mem->set('sou',$data,60*60); } } } else { $mem->set($sou,'1',60*60); } } $info = $mem->get('sou'); $minPrice=intval(Yii::$app->request->post('minPrice')); $maxPrice=intval(Yii::$app->request->post('maxPrice')); $minTime=Yii::$app->request->post('minTime'); $minTime=strtotime($minTime); $maxTime=Yii::$app->request->post('maxTime'); $maxTime=strtotime($maxTime); $cl = new SphinxClient; $goodsmodel = new Goods; $cl ->_limit=$goodsmodel->find()->count(); $cl ->SetServer('127.0.0.1',9312); $cl ->SetConnectTimeout(3); $cl ->SetArrayResult(true); if($key!=null){ $cl -> SetMatchMode(SPH_MATCH_ANY); }else{ $cl -> SetMatchMode(SPH_MATCH_FULLSCAN); } //var_dump($num);die; if($minPrice && $maxPrice){ $cl -> SetFilterRange('g_price',$minPrice,$maxPrice); } if($minTime && $maxTime){ $cl -> SetFilterRange('g_time',$minTime,$maxTime); } //echo $pages->offset,$pages->limit;die; //$cl ->SetLimits($pages->offset,$pages->limit); $res=$cl ->query($key,'mysql_goods'); $pages = new Pagination(); $pages ->totalCount=$res['total']; $pages ->defaultPageSize=3; if(isset($res['matches'])){ foreach($res['matches'] as $k=>$v){ $ids[]=$v['id']; } $data=Goods::find()->where(['in','g_id',$ids])->limit($pages->limit)->offset($pages->offset)->asArray()->all(); if($key!=null){ foreach($data as $k=>$v){ $data[$k]['g_name']=$cl ->BuildExcerpts([$v['g_name']],'mysql_goods',$key,['before_match'=>"<font style='font-weight:bold;color:red;'>",'after_match'=>"</font>"])[0]; } } //var_dump($data);die; }else{ if($key!=null){ $data="未搜索到关于<font style='font-weight:bold;color:red;'>{$key}</font>的数据"; }else{ $data="没有数据"; } } if(Yii::$app->request->isAjax){ return $this->renderPartial('goods',['pages'=>$pages,'data'=>$data,'minPrice'=>$minPrice,'maxPrice'=>$maxPrice,'minTime'=>$minTime,'maxTime'=>$maxTime,'key'=>$key,'info'=>$info]); } return $this->render('goods',['pages'=>$pages,'data'=>$data,'minPrice'=>$minPrice,'maxPrice'=>$maxPrice,'minTime'=>$minTime,'maxTime'=>$maxTime,'key'=>$key,'info'=>$info]); } } ?> 视图层 <?php use yiiwidgetsLinkPager; use yiihelpersHtml; use yiiwidgetsActiveForm; use yiiwebView; ?> <div id='cont'> <?php $form = ActiveForm::begin() ?> <input type="text" name="key" placeholder="<?=$key ?>" > <input type="text" name="minPrice" placeholder="<?=$minPrice ?>" >minPrice <input type="text" name="maxPrice" placeholder="<?=$maxPrice ?>" >maxPrice <input type="text" name="minTime" placeholder="<?=$minTime ?>" >minTime <input type="text" name="maxTime" placeholder="<?=$maxTime ?>" >maxTime <?= Html::submitButton('搜索', ['class'=>'btn btn-primary']) ?> <?php ActiveForm::end() ?> <div> <p> <?php if($info){ foreach ($info as $key => $v) {?> <font color="red"><?php echo $v;?></font> <?php } }?> </p> </div> <?php if(is_array($data)){ ?> <table border="1"> <tr> <th>name</th> <th>price</th> <th>date</th> </tr> <?php foreach($data as $k=>$v){ ?> <tr> <th><?=$v['g_name'] ?></th> <th><?=$v['g_price'] ?></th> <th><?=$v['g_time'] ?></th> </tr> <?php } ?> </table> <?php }else{ ?> <p><?=$data ?></p> <?php } ?> <?php echo LinkPager::widget(['pagination'=>$pages]); ?> </div> <?php $this->beginBlock('abc') ?> $(document).on('click','.pagination a',function(e){ e.preventDefault(); var url=$(this).attr('href'); var key=$(':input[name=key]').attr('placeholder'); var minPrice=$(':input[name=minPrice]').attr('placeholder'); var maxPrice=$(':input[name=maxPrice]').attr('placeholder'); var minTime=$(':input[name=minTime]').attr('placeholder'); var maxTime=$(':input[name=maxTime]').attr('placeholder'); var data='{'; if(key!=''){ data+="'key':'"+key+"',"; } if(minPrice!=''){ data+="'minPrice':'"+minPrice+"',"; } if(maxPrice!=''){ data+="'maxPrice':'"+maxPrice+"',"; } if(minTime!=''){ data+="'minTime':'"+minTime+"',"; } if(maxTime!=''){ data+="'maxTime':'"+maxTime+"',"; } data+='}'; //alert(data); data=eval('('+data+')'); $.post(url,data,function(msg){ $('#cont').html(msg); }); }); <?php $this->endBlock(); $this->registerJs($this->blocks['abc'],View::POS_END)?> </div>