//定每頁顯示多少條
//count(*)查詢總條數
//計算總頁數ceil()
//判斷$_GET 並賦值當前頁數
//mysql查詢 limit (當前頁數-1)*每頁條數,每頁條數
//如果總條數不為空,進行循環mysql_fetch_array()
//$_SERVER[‘PHP_SELF’]
//上一頁?page=當前頁-1
//下一頁?page=當前頁+1
<?php
include("conn.php");
$perpagenum = 3;
$total = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM message"));
$Total = $total[0];
$Totalpage =ceil($Total/$perpagenum);
if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage){
$page = 1;
}else{
$page = $_GET['page'];
}
$start = ($page-1)*$perpagenum;
$sql = "SELECT * FROM message ORDER BY id DESC LIMIT $start,$perpagenum";
$result = mysql_query($sql);
$contents = mysql_fetch_array($result);
?>
<table width="849" height="90">
<?php
if($total){
do{
$user = $contents['user'];
$contact = $contents['contact'];
$content = $contents['content'];
$date = $contents['date'];
?>
<tr bgcolor="#B3DBB5">
<td width="193" height="22">昵稱:<?php echo $user;?></td>
<td width="373"><?php echo $contact;?></td>
<td width="261">發表於:<?php echo $date;?></td>
</tr>
<tr bgcolor="#D8D8D8">
<td height="auto" colspan="3"><?php echo $content;?></td>
</tr>
<?php
}
while($contents=mysql_fetch_array($result));
$per = $page-1;
$next= $page+1;
echo "<center>共有".$Total."條留言,每頁".$perpagenum."條,共".$page."/".$Totalpage."頁。";
if($page!=1){
echo "<a href=".$_SERVER['PHP_SELF'].">首頁</a>".
"<a href=".$_SERVER['PHP_SELF']."?page=$per".">上一頁</a>";
}
if($page!=$Totalpage){
echo "<a href=".$_SERVER['PHP_SELF']."?page=$next".">下一頁</a>";
echo "<a href=".$_SERVER['PHP_SELF']."?page=$Totalpage".">尾頁</a>";
}
?>
<?php
}
?>
</table>