<?php
/*
需求,建立一個test數據庫,在裡邊建一個test表,裡面就
只要id字段,輸入一下數據就可以啦。。
由於水平有限,難免出錯。。
*/
$conn = mysql_connect("localhost","root","");
$maxnum = 2; //每頁顯示記錄條數
mysql_select_db("test", $conn);
$query1 = "SELECT COUNT(*) AS totalrows FROM test ";
$result1 = mysql_query($query1, $conn) or die(mysql_error());
$row1 = mysql_fetch_assoc($result1);
$totalRows1 = $row1['totalrows']; //數據集數據總條數
$totalpages = ceil($totalRows1/$maxnum);//計算可分頁總數,ceil()為上捨函數
if(!isset($_GET['page']) || !intval($_GET['page']) || $_GET['page'] > $totalpages) $page = 1; //對3種出錯進行默認處理
//在url參數page不存在時,page不為10進制數時,page大於可分頁數時,默認為1
else $page = $_GET['page'];
$startnum = ($page - 1)*$maxnum; //從數據集第$startnum條開始取,注意數據集是從0開始的
$query = "SELECT * FROM test LIMIT $startnum,$maxnum";//選擇出符合要求的數據 從$startnum條數據開始,選出$maxnum行
$result = mysql_query($query, $conn) or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>分頁示例</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
//-->
</script>
<style type="text/css">
a{text-decoration:none;}
a:hover{text-decoration:underline}
table{font-size:12px;}
.tb{background-color:#73BB95}
.tr{background-color:#FFFFFF}
</style>
</head>
<body>
<table width="30%" border="0" align="center" cellpadding="0" cellspacing="1" class="tb">
<tr>
<td height="24"><div align="left">分頁示例</div></td>
</tr>
<?php if($totalRows1) {//記錄集不為空顯示
do {
?>
<tr class="tr">
<td height="24"><div align="center"><?php echo $row['id'];?></div></td>
</tr>
<?php }while($row = mysql_fetch_assoc($result));?>
</table>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><form name="form1">
<td height="27"><div align="center">
<?php
echo "共計<font color=\"#ff0000\">$totalRows1</font>條記錄";
echo "<font color=\"#ff0000\">".$page."</font>"."/".$totalpages."頁 ";
//實現 << < 1 2 3 4 5> >> 分頁鏈接
$pre = $page - 1;//上一頁
$next = $page + 1;//下一頁
$maxpages = 4;//處理分頁時 << < 1 2 3 4 > >>顯示4頁
$pagepre = 1;//如果當前頁面是4,還要顯示前$pagepre頁,如<< < 3 /4/ 5 6 > >> 把第3頁顯示出來
if($page != 1) { echo "<a href='".$_SERVER['PHP_SELF']."'><<</a> ";
echo "<a href='".$_SERVER['PHP_SELF'].'?page='.$pre."'><</a> ";}
if($maxpages>=$totalpages) //如果總記錄不足以顯示4頁
{$pgstart = 1;$pgend = $totalpages;}//就不所以的頁面打印處理
elseif(($page-$pagepre-1+$maxpages)>$totalpages)//就好像總頁數是6,當前是5,則要把之前的3 4 顯示出來,而不僅僅是4
{$pgstart = $totalpages - $maxpages + 1;$pgend = $totalpages;}
else{
$pgstart=(($page<=$pagepre)?1:($page-$pagepre));//當前頁面是1時,只會是1 2 3 4 > >>而不會是 0 1 2 3 > >>
$pgend=(($pgstart==1)?$maxpages:($pgstart+$maxpages-1));
}
for($pg=$pgstart;$pg<=$pgend;$pg++){ //跳轉菜單
if($pg == $page) echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pg\"><font color=\"#ff0000\">$pg</font></a> ";
else echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pg\">$pg</a> ";
}
if($page != $totalpages)
{echo "<a href='".$_SERVER['PHP_SELF'].'?page='.$next."'>></a> ";
echo "<a href='".$_SERVER['PHP_SELF'].'?page='.$totalpages."'>>></a> ";}
?>
<select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
<option value="">選擇</option>
<?php for($pg1=1;$pg1<=$totalpages;$pg1++) {
echo "<option value=\"".$_SERVER['PHP_SELF']."?page=$pg1\">".$pg1."</option>";
}?>
</select>
</td></form>
</tr>
</table>
<?php } else {//記錄集為空時顯示?>
<tr class="tr">
<td height="24"><div align="center">沒有任何記錄</div></td>
</tr>
</table>
<?php }?>
</body>
</html>
<?php
mysql_free_result($result1);
mysql_free_result($result);
?>