php实现分页
php函数
<?php
//////////////////
//$result :容器的id
//$url:请求的url
//$total:总条数
//$num:页容量
//$pagenum:总页数
//$page:当前页
//$prepg:上一页
//$nextpg:下一页
//offset:limit后面的那个
////////////////
function ajaxPage($result,$url,$total,$num=20)
{
global $page,$pagenum,$prepg,$nextpg,$offset;
$GLOBALS["num"]=$num;
$page=isset($_GET['page'])?intval($_GET['page']):1; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
//$url='test1.php';//获取本页URL
$pagenum=ceil($total/$num); //获得总页数,也是最后一页
$page=min($pagenum,$page);//获得首页
$prepg=$page-1;//上一页
$nextpg=($page==$pagenum ? 0 : $page+1);//下一页
$offset=($page-1)*$num;
//开始分页导航条代码:
if($total==$offset+1){
$pagenav="显示第 <B>".$total."</B> 条记录,共 $total 条记录 ";
}else{
$pagenav="
显示第
<B>".($total?($offset+1):0)."</B>-<B>".min($offset+$num,$total)."</B>
条记录,共 $total 条记录 ";
}
//如果只有一页则跳出函数:
if($pagenum<1)
{
return false;
}
//获取url
if(strpos($url,"?")){
$firstPg =$url."&page=1";
$prePg = $url."&page=".$prepg;
$nextPg = $url."&page=".$nextpg;
$lastPg=$url."&page=".$pagenum;
}else{
$firstPg =$url."?page=1";
$prePg = $url."?page=".$prepg;
$nextPg = $url."?page=".$nextpg;
$lastPg=$url."?page=".$pagenum;
}
$pagenav.=" <a href=javascript:dopage('$result','$firstPg');>首页</a> ";
if($prepg) $pagenav.=" <a href=javascript:dopage('$result','$prePg');>上一页</a> "; else $pagenav.=" 上一页 ";
if($nextpg) $pagenav.=" <a href=javascript:dopage('$result','$nextPg');>下一页</a> "; else $pagenav.=" 下一页";
$pagenav.=" <a href=javascript:dopage('$result','$lastPg');>末页</a> ";
$pagenav.="</select> 页,共 $pagenum 页";
return $pagenav;
//假如传入的页数参数大于总页数,则显示错误信息
if($page>$pagenum){
Echo "Error : Can Not Found The page ".$page;
Exit;
}
//echo $num;
}
?>
把以上代码存为一个文件命名为ajaxPage.php
js代码
var http_request=false;
function send_request(url)
{
http_request=false;
//开始初始化XMLHttp对象
if(window.XMLHttpRequest){//MOILLA浏览器
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType("text/xml");
}
}
else if(window.ActiveXObject){//IE 浏览器
try{
http_request=new ActiveXObject("Msxml2.XMLHttp");
}catch(e){
try{
http_request=new ActiveXObject("Microsoft.XMLHttp");
}catch(e){}
}
}if(!http_request){
window.alert("创建XMLHttp对象失败");
return false;
}
http_request.onreadystatechange=processrequest;
//确定发送请求方式,URL,及是否同步执行下段代码
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息函数
function processrequest(){
//alert(reobj);
if(http_request.readyState==4){
if(http_request.status==200){
//alert(http_request.responseText);
document.getElementById(reobj).innerHTML=http_request.responseText;
//alert(http_request.responseText);
}else{
alert("您所请求的页面不正常");
}
}
}
function dopage(obj,url){
//alert(222);
document.getElementById(obj).innerHTML="正在读取数据.....";
reobj=obj;
send_request(url);
}
把以上代码存为一个文件命名为ajaxPage.js
调用:
//sql语句
$query1
= "select * from domainpay,alldomain where domainpay.domainId =
alldomain.domainId and UNIX_TIMESTAMP('{$startTime}') <=
UNIX_TIMESTAMP(payTime) and UNIX_TIMESTAMP(payTime) <=
UNIX_TIMESTAMP('{$endTime}') and status = 'active' and payTime is not
NULL";
}
//执行sql语句
$db -> execute($query);
//提取结果集
$result = $db -> fetch();
//计算总条数
$total = $db -> recordNum;
//分页函数的调用
$pagenav1
=
ajaxPage("supplement","isupplement.php?start={$_GET['start']}&end={$_GET['end']}&ac={$_GET['ac']}",$total,2);
$query.=" limit $offset,$num";
$db ->execute($query);
$result = $db -> fetch();
$db 是实例化的一个操作数据库的类!大家用的时候按照自己的即可!