PHP分頁執行個體代碼
<!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=gb2312" />
<title>php教程 make page list</title>
<style type="text/CSS">
<!--
.page a:link {
color: #0000FF;
text-decoration: none;
}
.page a:visited {
text-decoration: none;
color: #0000FF;
}
.page a:hover {
text-decoration: none;
color: #0000FF;
}
.page a:active {
text-decoration: none;
color: #0000FF;
}
.page{color:#0000FF;}
-->
</style>
</head>
<body>
<table width="530" height="103" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<th width="30" height="38" bgcolor="#E3E3E3" scope="col">ID</th>
<th width="500" bgcolor="#E3E3E3" scope="col">文章標題</th>
</tr>
<?php
/*
* Created on 2010-4-17
*
* Order by Kove Wong
*/
$link=MySQL_connect(’localhost’,’root’,’haoxiazai1987’);
mysql教程_select_db(’pagelist’);
mysql_query(’set names gbk’);
$Page_size=10;
$result=mysql_query(’select * from v_char’);
$count = mysql_num_rows($result);
$page_count = ceil($count/$Page_size);
$init=1;
$page_len=7;
$max_p=$page_count;
$pages=$page_count;
//判斷當前頁碼
if(empty($_GET[’page’])||$_GET[’page’]<0){
$page=1;
}else {
$page=$_GET[’page’];
}
$offset=$Page_size*($page-1);
$sql="select * from v_char limit $offset,$Page_size";
$result=mysql_query($sql,$link);
while ($row=mysql_fetch_array($result)) {
?>
<tr>
<td bgcolor="#E0EEE0" height="25px"><div align="center">
<?php echo $row[’id’]?>
</div></td>
<td bgcolor="#E0EEE"><div align="center">
<?php echo $row[’name’]?>
</div></td>
</tr>
<?php
}
$page_len = ($page_len%2)?$page_len:$pagelen+1;//頁碼個數
$pageoffset = ($page_len-1)/2;//頁碼個數左右位移量
$key=’<div class="page">’;
$key.="<span>$page/$pages</span> "; //第幾頁,共幾頁
if($page!=1){
$key.="<a href="".$_SERVER[’PHP_SELF’]."?page=1">第一頁</a> "; //第一頁
$key.="<a href="".$_SERVER[’PHP_SELF’]."?page=".($page-1)."">上一頁</a>"; //上一頁
}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.=’ <span>’.$i.’</span>’;
} else {
$key.=" <a href="".$_SERVER[’PHP_SELF’]."?page=".$i."">".$i."</a>";
}
}
if($page!=$pages){
$key.=" <a href="".$_SERVER[’PHP_SELF’]."?page=".($page+1)."">下一頁</a> ";//下一頁
$key.="<a href="".$_SERVER[’PHP_SELF’]."?page={$pages}">最後一頁</a>"; //最後一頁
}else {
$key.="下一頁 ";//下一頁
$key.="最後一頁"; //最後一頁
}
$key.=’</div>’;
?>
<tr>
<td colspan="2" bgcolor="#E0EEE0"><div align="center"><?php echo $key?></div></td>
</tr>
</table>
</body>
</html>