Example of paging functionality implemented by thinkPHP 3.2. 3 in conjunction with Laypage
- 2021-10-13 06:59:53
- OfStack
This article illustrates the paging function of thinkPHP 3.2. 3 combined with Laypage. Share it for your reference, as follows:
Controller
<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
/**
*@brief Query
****/
public function index(){
$choose = I('choose','-6');
$c['easy_hard'] = $choose;
$type=I('typeid','');
$nowpage=I('page',1);
if($type == '')
{
if($choose == -6)
{
$totalpage=ceil((D('data')->count())/10);
$infos=D('data')->limit(($nowpage-1)*10,10)->select();
}else{
$totalpage=ceil((D('data')->where($c)->count())/10);
$infos=D('data')->where($c)->limit(($nowpage-1)*10,10)->select();
}
}else{
if($choose == -6)
{
$map['data'] = array('like',"%$type%");
$totalpage=ceil((D('data')->where($map)->count())/10);
$infos=D('data')->where($map)->limit(($nowpage-1)*10,10)->select();
}else{
$map['data'] = array('like',"%$type%");
$totalpage=ceil((D('data')->where($map)->where($c)->count())/10);
$infos=D('data')->where($map)->where($c)->limit(($nowpage-1)*10,10)->select();
}
}
$this->assign('type',$type);
$this->assign('choose',$choose);
$this->assign("totalpage",$totalpage);
$this->assign("infos",$infos);
$this -> display();
}
}
View layer
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Think Demo</title>
<script type="text/javascript" src="__PUBLIC__/jquery-1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="__PUBLIC__/jquery-1.11.1/jquery.js"></script>
<script type="text/javascript" src="__PUBLIC__/layer/layer.js"></script>
<script type="text/javascript" src="__PUBLIC__/laypage/laypage.js"></script>
</head>
<body>
<div>
<select name="" id="slc1" onchange="return choose()">
<option value="-6" <if condition="$choose eq -6 "> selected </if> > All </option>
<option value="0" <if condition="$choose eq 0 "> selected </if> > Simple </option>
<option value="1" <if condition="$choose eq 1 "> selected </if> >1 Like </option>
</select>
<input type="text" value="<if condition="$type neq '' "> {$type} </if>" id="type"><button id="sou"> Search </button>
</div>
<br>
<table border="1" width="500" height="150" >
<tr>
<th>ID</th>
<th> Language </th>
<th> Difficulty degree </th>
<th> Operation </th>
</tr>
<volist name="infos" id="vo">
<tr>
<th>{$vo.id}</th>
<th>{$vo.data}</th>
<th>
<if condition="$vo.easy_hard eq '0'"> Simple
<else />1 Like
</if>
</th>
<th>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="return del({$vo.id});"> Delete </a>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="return edit({$vo.id});"> Modify </a>
</th>
</tr>
</volist>
</table>
<div style="margin-top:15px; text-align:center;" id="page11"></div>
<button onclick="return add_()"> Add </button> <br />
<script type="text/javascript">
function choose()
{
var type=$("#type").val();
var checkValue=$("#slc1").val();
window.location.href="?typeid=" rel="external nofollow" rel="external nofollow" +type+"&choose="+checkValue;
}
$("#sou").bind("click",function(event){
var type=$("#type").val();// Get the assumed search condition value
var checkValue=$("#slc1").val();
window.location.href="?typeid=" rel="external nofollow" rel="external nofollow" +type+'&choose='+checkValue;
});
$(function(){
laypage({
cont: 'page11',
pages: {$totalpage}, // Suppose what we get is 18 After the back end calculates the total number of pages, pass the total page value and put it here (similar to {$totalpage} )) .
curr: function(){ // Pass url Gets the current page, or the same as above ( pages )
var page = location.search.match(/page=(\d+)/);
return page ? page[1] : 1;// If no pages are displayed, the default is the 1 Page
}(),
jump: function(e, first){ // Trigger callback after paging
if(!first){ //1 Be sure to add this judgment, otherwise it will be refreshed indefinitely at the beginning
location.href=setParam("page",e.curr);
}
}
});
});
function setParam(param,value){
var query = location.search.substring(1);
var p = new RegExp("(^|)" + param + "=([^&]*)(|$)");
if(p.test(query)){
//query = query.replace(p,"$1="+value);
var firstParam=query.split(param)[0];
var secondParam=query.split(param)[1];
if(secondParam.indexOf("&")>-1){
var lastPraam=secondParam.split("&")[1];
return '?'+firstParam+'&'+param+'='+value+'&'+lastPraam;
}else{
if(firstParam){
return '?'+firstParam+''+param+'='+value;
}else{
return '?'+param+'='+value;
}
}
}else{
if(query == ''){
return '?'+param+'='+value;
}else{
return '?'+query+'&'+param+'='+value;
}
}
}
</script>
</body>
</html>
Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.