Example of Yii Framework Introducing coreseek Paging Function
- 2021-11-24 01:00:14
- OfStack
This paper illustrates the introduction of coreseek paging function into Yii framework. Share it for your reference, as follows:
Change sphinxapi. php to SphinxClient. php file and put it anywhere you can find it. I put it in advanced/frontend/web/SphinxClient. php and open common/config/bootstrap. php
Add in it
Yii::$classMap['SphinxClient']='@frontend/web/SphinxClient.php';
The address is written correctly
In need of control, use SphinxClient
controller Controller
/**
* Topic search
*
* @author YING
* @param void
* @return void
*/
public function actionTopic()
{
// Analog data
$studId=2; // Users id
$classId=2; // Class id
$title=""; // Empty
// Instantiation model
$studTopic=new StudTopic();
// Query
$data=$studTopic->find()->select('*')->innerJoin('stud_user','stud_topic.stud_id=stud_user.stud_id')->where(['class_id'=>$classId]);
// Instantiate the paging class
$pagination=new Pagination(['totalCount' => $data->count()]);
// Number of strips per page
$pagination->setPageSize(3);
// Perform paging
$topicInfo= $data->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();
// Return value
return $this->render('topicList',['topicInfo'=>$topicInfo,'pages'=>$pagination,'studId'=>$studId,'classId'=>$classId,'title'=>$title]);
}
/**
* coreseek Search
*
* @author YING
* @param void
* @return void
*/
public function actionSearchTitle()
{
// Connection value
$title=Yii::$app->request->get('t_title');
$classId=Yii::$app->request->get('class_id');
// Analog data
$studId=2; // Users id
//coreseek Search
$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 there is a value
if($res['total']){
$matches=$res['matches'];
foreach($matches as $key => $val){
$tidArray[]=$val['id'];
}
}
// Convert to a string
$tidStr=isset($tidArray) ? implode(',',$tidArray) : 0;
// Instantiation model
$studTopic=new StudTopic();
// Query
$data=$studTopic->find()->select('*')->innerJoin('stud_user','stud_topic.stud_id=stud_user.stud_id')->where("t_id in ($tidStr)");
// Instantiate the paging class
$pagination=new Pagination(['totalCount' => $data->count()]);
// Number of strips per page
$pagination->setPageSize(3);
// Perform paging
$topicInfo= $data->offset($pagination->offset)->limit($pagination->limit)->asArray()->all();
// Load template
return $this->render('topicList',['topicInfo'=>$topicInfo,'pages'=>$pagination,'studId'=>$studId,'classId'=>$classId,'title'=>$title]);
}
view View
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\LinkPager;
?>
<table class="table">
<tr>
<td> Title </td>
<td> Author </td>
<td> Release time </td>
<td> Operation </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" > Edit </a>||<a href=""> Delete </a></td>
<?php else: ?>
<td><a href=""> Delete </a></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
<tr>
<td><input type="button" value=" All selection / Do not choose at all " id="all"/></td>
<td><input type="button" value=" Reverse selection " id="fan"/></td>
<td><input type="button" value=" Batch deletion " id="del"/></td>
</tr>
</table>
<?php
echo LinkPager::widget([
'pagination' => $pages,
]);
?>
<script src="./css/js/jquery.1.12.min.js"></script>
<script>
// All selection / Do not choose at all
var temp=true; // Temporary variable
$('#all').click(function(){
$('input[type="checkbox"]').prop('checked',temp);
// Inversion
temp=!temp;
})
// Batch deletion
$('#del').click(function(){
var checkAll=$('input[type="checkbox"]'); // Get all check boxes
var length=checkAll.length; // Calculated length
var arr=new Array(); // Defining Array
var str=""; // Definition string
// Cycle
$.each(checkAll,function(k,v){
// Determine whether it is selected
if(checkAll[k].checked){
arr.push(checkAll.eq(k).attr('tid'));
}
})
// Convert to a string
str=arr.join(',');
//ajax
var url="index.php?r=student/delete-all"; // Address
$.get(url,{str:str},function(msg){
if(msg){
//window.location.reload(); // Refresh page
// Node deletion
$.each(arr,function(k,v){
$('#tr_'+v).remove();
});
}
},'json');
});
// Reverse selection
$("#fan").click(function(){
var checkAll=$('input[type="checkbox"]'); // Get check
$.each(checkAll,function(k,v){
this.checked=!this.checked;
})
});
</script>
Done, call it a day ok!
For more readers interested in Yii related content, please check the topics on this site: "Introduction to Yii Framework and Summary of Common Skills", "Summary of Excellent Development Framework of php", "Introduction to smarty Template", "Introduction to php Object-Oriented Programming", "Summary of Usage of php String (string)", "Introduction to php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"
I hope this article is helpful to the PHP programming based on Yii framework.