<!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分頁</title>
</head>
<body>
<?php
$conn=mysql_connect("localhost","root","123456") or die("資料庫連接錯誤".mysql_error());
mysql_select_db("wangyantest",$conn) or die("資料庫訪問錯誤".mysql_error());
mysql_query("set names gb2312");
$page=$_GET["page"];
if($page=="")
{$page=1;}
if(is_numeric($page))
{
$page_size=2; //每頁多少條資料
$query="select count(*) as total from test order by id desc";
$result=mysql_query($query);
$message_count=mysql_result($result,0,"total");
$page_count=ceil($message_count/$page_size);
$offset=($page-1)*$page_size;
$sql=mysql_query("select * from test order by id desc limit $offset,$page_size");
$row=mysql_fetch_object($sql);
if(!$row)
{
echo("暫無任何資料!");
}
?>
<ul>
<?php
do
{
?>
<li><?php echo "<a href=contnet.php?id=".$row->id.">". $row->titile."|".$row->content."</a>" ?></li>
<?php
}while($row=mysql_fetch_object($sql));
?>
</ul>
<?php
}
?>
當前頁碼:<?php echo $page;?>/<?php echo $page_count;?>
記錄條數:<?php echo $message_count;?>
<hr/>
<?php
if($page!=1)
{
echo "<a href=page.php?page=1>首頁</a> | ";
echo "<a href=page.php?page=".($page-1).">上一頁</a> ";
}
if($page<$page_count)
{
echo "<a href=page.php?page=".($page+1).">下一頁</a> | ";
echo "<a href=page.php?page=".$page_count.">尾頁</a>";
}
mysql_free_result($sql);
mysql_close($conn);
?>
</body>
</html>