<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>結合jquery作ajax分頁</title>
<style>
body{ font-size:12px; font-family:Verdana, Arial, Helvetica, sans-serif}
.page A{ padding:3px 5px; float:left; border:solid #CCCCCC 1px; text-decoration:none; font-size:12px; margin-left:1px; font-family:Verdana;color:#000000;}
.page A.currentpage{ color:#FF0000; border: solid red 1px;}
.page A:hover{ background-color:#CCCCCC;}
</style>
<script language="javascript" src="jquery-1.9.1.js"></script>
</head>javascript
<body>
<?php
include_once('conn.php');
//PHP分頁實例php
//分頁尺寸
$page_size=2;
//計算總行數html
$total_records=mysql_num_rows(mysql_query("SELECT id from user"));java
//總頁數
$total_page=ceil($total_records/$page_size);
$query=mysql_query("select * from user limit 0,$page_size");
?>
<div class="recordlist">
<ul>
<?
while ($rs=mysql_fetch_array($query))
{
?>
<li><?=$rs["id"]?>-<?=$rs["user"]?></li>mysql
<?
}
?>
</ul>
</div>
<div class='page'></div>jquery
</body>
</html>
<script language="javascript">
var total_page=<?=$total_page?>;
var url="page5.php?page";//當前連接,格式以下,例如:?aa=1&page ,somepage.php?action=1&cat=1&pageajax
$().ready(function(){sql
$(".page").html(pagination(1))
page_link();
})fetch
function page_link()
{
$(".page A").click(function(){
var page=parseInt($(this).attr("page"))
$(".recordlist").load($(this).attr("href"),"",function(){$(".page").html(pagination(page));page_link();})
return false;
})
}ui
function pagination(current_page)
{
//翻頁連接開始
current_page = current_page<=0 ? 1 : current_page;
current_page = current_page>total_page?total_page:current_page;
var page_link="";
if (total_page>1)
{
page_link="<a>一共"+total_page+"頁</a>";
page_link+="<a>當前第"+current_page+"頁</a>";
page_link+="<a href="+url+"=1 page=1>首頁</a>";
if (current_page>1){
//頁碼大於1的時候,顯示上一頁翻頁連接
var pre_page=current_page-1;
page_link+="<a href='"+url+"="+pre_page+"' page="+pre_page+"><<</a>";
}
//翻頁列表
//步進分頁,翻頁列表的數字始終只顯示9個或者自定義的個數,本例定義只顯示9個,當前頁左右各顯示4個頁碼
if (total_page>9)
{
if (current_page>4)
{
var from=current_page-4;
var to=current_page+4;
if (to>total_page)
{
var from=total_page-8;
var to=total_page;
}
}
else
{
var from=1;
var to=9;
}
}
else
{
var from=1;
var to=total_page;
}
for (var i=from;i<=to;i++)
{
if (i==current_page)
{
//高亮當前頁頁碼
page_link+="<a href='"+url+"="+i+"' class='currentpage' page="+i+">"+i+"</a>";
}
else
{
page_link+="<a href='"+url+"="+i+"' page="+i+" page="+i+">"+i+"</a>";
}
}
//頁碼小於總頁數的時候顯示下一頁翻頁連接
var next_page=current_page+1;
if(next_page<total_page)
{
page_link+="<a href='"+url+"="+next_page+"' page="+next_page+">>></a>";
}
page_link+="<a href='"+url+"="+total_page+"' page="+total_page+">最後一頁</a>";
}
return page_link;
}
</script>
<?
include_once('conn.php');
$page1=$_GET["page"]+0;
if(isset($page1)){
$page=$page1;
}else{
$page=1;
}
//分頁尺寸
$page_size=3;
$offset=($page-1)*$page_size;
$q=mysql_query("select * from user limit $offset,$page_size");
?>
<ul>
<?
while ($r=mysql_fetch_array($q))
{
?>
<li><?php echo $r["id"]?>-<? echo $r["user"]?></li>
<?}?></ul>