html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src='__PUBLIC__/jquery-3.0.0.min.js'></script>
</head>
<body>
<div id="list">
<ul></ul>
</div>
<div id="pagecount"></div>
</body>
</html>
<script>
var curPage = 1; //当前页码
var total,pageSize,totalPage; //总记录数,每页显示数,总页数
//获取数据
function getData(page){
$.ajax({
type: 'POST',
url: "{:U('Index/ajaxs')}",
data: {'pageNum':page-1},
dataType:'json',
beforeSend:function(){
$("#list ul").append("<li id='loading'>loading...</li>");//显示加载动画
},
success:function(json){
$("#list ul").empty();//清空数据区
total = json.total; //总记录数
pageSize = json.pageSize; //每页显示条数
curPage = page; //当前页
totalPage = json.totalPage; //总页数
var li = "";
var list = json.list;
$.each(list,function(index,array){ //遍历json数据列
li += "<li><a href='#'>"+array['id']+"</a></li>";
});
$("#list ul").append(li);
},
complete:function(){ //生成分页条
getPageBar();
fun();
},
error:function(){
alert("数据加载失败");
}
});
}
//获取分页条
function getPageBar(){
//页码大于最大页数
if(curPage>totalPage) curPage=totalPage;
//页码小于1
if(curPage<1) curPage=1;
pageStr = "<span>共"+total+"条</span><span>"+curPage
+"/"+totalPage+"</span>";
//如果是第一页
if(curPage==1){
pageStr += "<span>首页</span><span>上一页</span>";
}else{
pageStr += "<span><a href='javascript:void(0)' rel='1'>首页</a></span><span><a href='javascript:void(0)' rel='"+(curPage-1)+"'>上一页</a></span>";
}
//如果是最后页
if(curPage>=totalPage){
pageStr += "<span>下一页</span><span>尾页</span>";
}else{
pageStr += "<span><a href='javascript:void(0)' rel='"+(parseInt(curPage)+1)+"'> 下一页</a></span><span><a href='javascript:void(0)' rel='"+totalPage+"'>尾页</a></span>";
}
$("#pagecount").html(pageStr);
}
$(function(){
getData(1);
});
function fun(){
$("#pagecount span a").on('click',function(){
var rel = $(this).attr("rel");
alert(rel)
if(rel){
getData(rel);
}
});
}
</script>
PHP代码:
<?php
namespace HomeController;
use ThinkController;
class IndexController extends Controller {
public function index(){
echo 'ddd';
$this->display();
exit;
$result = mysql_query("select id from food");
$total = mysql_num_rows($result);//总记录数
$pageSize = 6; //每页显示数
$totalPage = ceil($total/$pageSize); //总页数
$startPage = $page*$pageSize; //开始记录
//构造数组
$arr['total'] = $total;
$arr['pageSize'] = $pageSize;
$arr['totalPage'] = $totalPage;
$query = mysql_query("select id,title,pic from food order by id asc limit
$startPage,$pageSize"); //查询分页数据
while($row=mysql_fetch_array($query)){
$arr['list'][] = array(
'id' => $row['id'],
'title' => $row['title'],
'pic' => $row['pic'],
);
}
echo json_encode($arr); //输出JSON数据
}
public function ajaxs(){
$page = intval($_POST['pageNum']); //当前页
$statistics=M('SearchStatistics');
$total=$statistics->count();//总记录数
$pageSize = 6; //每页显示数
$totalPage = ceil($total/$pageSize); //总页数
$startPage = $page*$pageSize; //开始记录
//构造数组
$arr['total'] = $total;
$arr['pageSize'] = $pageSize;
$arr['totalPage'] = $totalPage;
$data=$statistics->limit("$startPage,$pageSize")->select();
// dump($data);
$arr['list'] = $data;
echo json_encode($arr); //输出JSON数据
}
}