<!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>Untitled Document</title>
</head>
<body>
<style type="text/css">
*{
margin:0;
padding:0;
}
#lay{
margin:100px 100px 100px 100px;
}
a{
padding:5px;
font-size:13px;
text-decoration:none;
}
span{
font-size:13px;
}
table{
border:1px solid red;
border-collapse:collapse;
}
</style>
id
值
$link = mysql_connect("localhost","root",""); //連接數據庫
mysql_select_db("cs");//選擇哪個數據庫
mysql_query("set names utf-8"); //設置數據庫字符編碼為中文
$sql = mysql_query("select *from persons");
$pagesize = 5; //顯示條數
$sum = mysql_num_rows($sql); //判斷 一共有多少條數據
$count = ceil($sum/$pagesize);//求出一共有多少頁
$pages = $count; //顯示最後一頁
$init = 1;
$page_len = 7;
$max_p = $count;
if(empty($_GET["page"])|| $_GET["page"]<0){
$page = 1;
}else{
$page = $_GET["page"];
}
$off = ($page-1)*$pagesize; //求出數據庫查詢的第一個數據
$he = mysql_query("select * from persons limit $off,$pagesize");//按照off開始,到pagesize規律顯示
echo "一共 ".$sum." 條數據";
while($row = mysql_fetch_array($he)){
echo "";
echo "".$row['id']."";
echo "".$row['FirstName']."";
echo " ";
}
?>
$page_len = ($page_len%2)?$page_len:$page_len+1;//如果余為1則為真,為0則為假
$pageoffset = ($page_len-1)/2;//頁碼偏移量
$key ="";
if($page!=1){
$key.="第一頁 ";
$key.="上一頁 ";
}else{
$key.="第一頁 ";
$key.="上一頁 ";
}
if($pages>$page_len){
if($page<=$pageoffset){
$init=1;
$max_p = $page_len;
}else{
if($page+$pageoffset>=$pages+1){
$init = $pages - $page_len+1;
}else{
$init = $page-$pageoffset;
$max_p = $page + $pageoffset;
}
}
}
for($i=$init;$i<=$max_p;$i++){
if($i==$page){
$key.="[ ".$i." ]";
}else{
$key.="$i";
}
}
if($i-1!=$page){
$key.="下一頁";
$key.="最後一頁";
}else{
$key.="下一頁";
$key.="最後一頁";
}
echo "";
echo "".$key."";
echo "";
?>