当前位置: 移动技术网 > IT编程>开发语言>PHP > Yii框架引入coreseek分页功能示例

Yii框架引入coreseek分页功能示例

2019年03月19日  | 移动技术网IT编程  | 我要评论

蓝骏,肤雅乐,确山二手房网

本文实例讲述了yii框架引入coreseek分页功能。分享给大家供大家参考,具体如下:

把sphinxapi.php改为sphinxclient.php 类文件随便放,你能找到就行,我放在advanced/frontend/web/sphinxclient.php,打开common/config/bootstrap.php

在里面添加

yii::$classmap['sphinxclient']='@frontend/web/sphinxclient.php';

地址写正确

在需要用得控制其中 use sphinxclient

controller控制器

/**
 * 话题搜索
 *
 * @author ying
 * @param void
 * @return void
 */
public function actiontopic()
{
  //模拟数据
  $studid=2; //用户id
  $classid=2; //班级id
  $title=""; //为空
  //实例化模型
  $studtopic=new studtopic();
  //查询
  $data=$studtopic->find()->select('*')->innerjoin('stud_user','stud_topic.stud_id=stud_user.stud_id')->where(['class_id'=>$classid]);
  //实例化分页类
  $pagination=new pagination(['totalcount' => $data->count()]);
  //每页条数
  $pagination->setpagesize(3);
  //执行分页
  $topicinfo= $data->offset($pagination->offset)->limit($pagination->limit)->asarray()->all();
  //返回值
  return $this->render('topiclist',['topicinfo'=>$topicinfo,'pages'=>$pagination,'studid'=>$studid,'classid'=>$classid,'title'=>$title]);
}
/**
 * coreseek搜索
 *
 * @author ying
 * @param void
 * @return void
 */
public function actionsearchtitle()
{
  //接值
  $title=yii::$app->request->get('t_title');
  $classid=yii::$app->request->get('class_id');
  //模拟数据
  $studid=2; //用户id
  //coreseek 搜索
  $cl = new sphinxclient ();
  $cl->setserver ( '127.0.0.1', 9312);
  $cl->setconnecttimeout ( 3 );
  $cl->setarrayresult ( true );
  $cl->setmatchmode ( sph_match_any);
  $res = $cl->query ( $title, "*" );
  //如果存在值
  if($res['total']){
    $matches=$res['matches'];
    foreach($matches as $key => $val){
     $tidarray[]=$val['id'];
    }
  }
  //转化为字符串
  $tidstr=isset($tidarray) ? implode(',',$tidarray) : 0;
  //实例化模型
  $studtopic=new studtopic();
  //查询
  $data=$studtopic->find()->select('*')->innerjoin('stud_user','stud_topic.stud_id=stud_user.stud_id')->where("t_id in ($tidstr)");
  //实例化分页类
  $pagination=new pagination(['totalcount' => $data->count()]);
  //每页条数
  $pagination->setpagesize(3);
  //执行分页
  $topicinfo= $data->offset($pagination->offset)->limit($pagination->limit)->asarray()->all();
  //加载模板
  return $this->render('topiclist',['topicinfo'=>$topicinfo,'pages'=>$pagination,'studid'=>$studid,'classid'=>$classid,'title'=>$title]);
}

view视图

<?php
use yii\widgets\activeform;
use yii\helpers\html;
use yii\helpers\url;
use yii\widgets\linkpager;
?>
<table class="table">
  <tr>
    <td>标题</td>
    <td>作者</td>
    <td>发布时间</td>
    <td>操作</td>
  </tr>
  <?php foreach($topicinfo as $key => $val): ?>
    <tr id="tr_<?= $val['t_id']?>">
      <td><input type="checkbox" tid="<?= $val['t_id']?>"/> <?= $val['t_title']?></td>
      <td><?= $val['stud_name']?></td>
      <td><?= date('y-m-d h:i:s',$val['add_time'])?></td>
      <?php if($val['stud_id']==$studid):?>
      <td><a href="index.php?r=student/update-topic&topic_id=<?= $val['t_id']?>" rel="external nofollow" >编辑</a>||<a href="">删除</a></td>
      <?php else: ?>
      <td><a href="">删除</a></td>
      <?php endif; ?>
    </tr>
  <?php endforeach; ?>
  <tr>
    <td><input type="button" value="全选/全不选" id="all"/></td>
    <td><input type="button" value="反选" id="fan"/></td>
    <td><input type="button" value="批删" id="del"/></td>
  </tr>
</table>
<?php
echo linkpager::widget([
  'pagination' => $pages,
]);
?>
<script src="./css/js/jquery.1.12.min.js"></script>
<script>
  //全选/全不选
   var temp=true; //临时变量
  $('#all').click(function(){
    $('input[type="checkbox"]').prop('checked',temp);
    //取反
    temp=!temp;
  })
  //批删
  $('#del').click(function(){
    var checkall=$('input[type="checkbox"]'); //获取全部的复选框
    var length=checkall.length; //计算长度
    var arr=new array(); //定义数组
    var str=""; //定义字符串
    //循环
    $.each(checkall,function(k,v){
      //判断是否选中
      if(checkall[k].checked){
        arr.push(checkall.eq(k).attr('tid'));
      }
    })
    //转化为字符串
    str=arr.join(',');
    //ajax
    var url="index.php?r=student/delete-all"; //地址
    $.get(url,{str:str},function(msg){
      if(msg){
        //window.location.reload(); //刷新页面
        //节点删除
        $.each(arr,function(k,v){
          $('#tr_'+v).remove();
        });
      }
    },'json');
  });
  //反选
  $("#fan").click(function(){
    var checkall=$('input[type="checkbox"]'); //获取复选
    $.each(checkall,function(k,v){
      this.checked=!this.checked;
    })
  });
</script>

搞定 收工 ok!

更多关于yii相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于yii框架的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网