$pagesize=10; //設置每一頁顯示的記錄數
$conn=mysql_connect("localhost","root",""); //連接數據庫
$rs=mysql_query("select count(*) from tb_product",$conn); //取得記錄總數$rs
$myrow = mysql_fetch_array($rs);
$numrows=$myrow[0];
//計算總頁數
$pages=intval($numrows/$pagesize);
//判斷頁數設置
if (isset($_get['page'])){
$page=intval($_get['page']);
}
else{
$page=1; //否則,設置為第一頁
}
create table mytable(id int not null auto_increment,news_title varchar(50),news_cont text,add_time datetime,primary key(id))
<html>
<head>
<title>php分頁示例</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<?php
$conn=mysql_connect("localhost","root","");
//設定每一頁顯示的記錄數
$pagesize=1;
mysql_select_db("mydata",$conn);
//取得記錄總數$rs,計算總頁數用
$rs=mysql_query("select count(*) from tb_product",$conn);
$myrow = mysql_fetch_array($rs);
$numrows=$myrow[0];
//計算總頁數
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages++;
//設置頁數
if (isset($_get['page'])){
$page=intval($_get['page']);
}
else{
//設置為第一頁
$page=1;
}
//計算記錄偏移量
$offset=$pagesize*($page - 1);
//讀取指定記錄數
$rs=mysql_query("select * from mytable order by id desc limit $offset,$pagesize",$conn);
if ($myrow = mysql_fetch_array($rs))
{
$i=0;
?>
<table border="0" width="80%">
<tr>
<td width="50%" bgcolor="#e0e0e0">
<p align="center">標題</td>
<td width="50%" bgcolor="#e0e0e0">
<p align="center">發布時間</td>
</tr>
<?php
do {
$i++;
?>
<tr>
<td width="50%"><?=$myrow["news_title"]?></td>
<td width="50%"><?=$myrow["news_cont"]?></td>
</tr>
<?php
}
while ($myrow = mysql_fetch_array($rs));
echo "</table>";
}
echo "<div align='center'>共有".$pages."頁(".$page."/".$pages.")";
for ($i=1;$i< $page;$i++)
echo "<a href='fenye.php?page=".$i."'>[".$i ."]</a> ";
echo "[".$page."]";
for ($i=$page+1;$i<=$pages;$i++)
echo "<a href='fenye.php?page=".$i."'>[".$i ."]</a> ";
echo "</div>";
?>
</body>
</html>
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
if ($page > 1)
{
echo "<a href='fenye.php?page=".$first."'>首頁</a> ";
echo "<a href='fenye.php?page=".$prev."'>上一頁</a> ";
}
if ($page < $pages)
{
echo "<a href='fenye.php?page=".$next."'>下一頁</a>
echo "<a href='fenye.php?page=".$last."'>尾頁</a> ";
}
新聞熱點
疑難解答