感覺這個也不錯,可以用來學習一下。
<?php
include("conn.php"); //數據庫配置文件
$pagesize=3; //設置每頁條數
$url=$_SERVER[REQUEST_URI];
$url=parse_url($url);
$url=$url[path]; //截取到php路徑,防止翻頁出錯
$num=mysql_num_rows(mysql_query("select * from `message`")); //計算總條數
$totalpage=ceil($num/$pagesize); //計算總頁數
$pageval=1; //定義$pageval初始值,避免出現第一次點擊下一頁時還在本頁
if($_GET[page])
{
$pageval=$_GET[page];
if($pageval<=1)$pageval=1;
$page=($pageval-1)*$pagesize;
$page.=",";
}
echo "<br>";
$sql="select * from `message` limit $page $pagesize";
$query=mysql_query($sql,$conn);
while($row=mysql_fetch_array($query))
{
echo $row[id]."\t".$row[user]."\t".$row[passw]."\t".$row[regdate]."<br>";
}
echo "頁次:$pageval/$totalpage 頁\t";
echo "<a href=".$url.">首頁</a>\t";
if($num > $pagesize)
{
if($pageval==1)
{
echo "<a>上一頁</a>\t";
echo "<a href='$url?page=".($pageval+1)."'>下一頁</a>\t";
echo "\t<a href=$url?page=".$totalpage.">尾頁</a>\t";
}else if($pageval < $totalpage)
{
echo "<a href='$url?page=".($pageval-1)."'>上一頁</a>\t";
echo "<a href='$url?page=".($pageval+1)."'>下一頁</a>\t";
echo "\t<a href=$url?page=".$totalpage.">尾頁</a>\t";
}else if($pageval == $totalpage)
{
echo "<a href='$url?page=".($pageval-1)."'>上一頁</a>\t";
echo "<a>[下一頁]</a>\t";
echo "\t<a href=$url?page=".$totalpage.">尾頁</a>\t";
}
}else
{
echo "<a>上一頁</a>\t";
echo "<a>下一頁</a>\t";
echo "\t<a>尾頁</a>\t";
}
echo "共計".$totalpage."頁";
?>