本文实例讲述了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控制器
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]);
}
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视图
| 标题 | 作者 | 发布时间 | 操作 | |
| = $val['t_title']?> | = $val['stud_name']?> | = date('Y-m-d H:i:s',$val['add_time'])?> | " rel="external nofollow" >编辑||删除 | 删除 |
搞定 收工 ok!
更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。



