分頁詳解 從此分頁無憂(PHP+mysql)
更新時間:2007年11月23日 22:13:53 作者:
本人所發(fā)代碼全部為本人原創(chuàng),并且應(yīng)用在多個項(xiàng)目中. 自己經(jīng)常用php+mysql分頁代碼
<?php
$perpagenum = 10;//定義每頁顯示幾條
$total = mysql_fetch_array(mysql_query("select count(*) from a"));//查詢數(shù)據(jù)庫中一共有多少條數(shù)據(jù)
$Total = $total[0]; //
$Totalpage = ceil($Total/$perpagenum);//上舍,取整
if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage)//page可能的四種狀態(tài)
{
$page=1;
}
else
{
$page=$_GET['page'];//如果不滿足以上四種情況,則page的值為$_GET['page']
}
$startnum = ($page-1)*$perpagenum;//開始條數(shù)
$sql = "select * from a order by id limit $startnum,$perpagenum";//查詢出所需要的條數(shù)
echo $sql."
";
$rs = mysql_query($sql);
$contents = mysql_fetch_array($rs);
if($total)如果$total不為空則執(zhí)行以下語句
{
do
{
$id = $contents['id'];
$name = $contents['name'];
?>
<table border="0" align="center">
<tr>
<td>id:
<?php echo $id;?>
</td>
</tr>
<tr>
<td>name:
<?php echo $name;?>
</td>
</tr>
</table>
<?php
}
while($contents = mysql_fetch_array($rs));//do....while
$per = $page - 1;//上一頁
$next = $page + 1;//下一頁
echo "<center>共有".$Total."條記錄,每頁".$perpagenum."條,共".$Totalpage."頁 ";
if($page != 1)
{
echo "<a href='".$_SERVER['PHP_SELF']."'>首頁</a>";
echo "<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></center>";
}
}
else如果$total為空則輸出No message
{
echo "<center>No message</center>";
}
?>
復(fù)制代碼 代碼如下:
<?php
$perpagenum = 10;//定義每頁顯示幾條
$total = mysql_fetch_array(mysql_query("select count(*) from a"));//查詢數(shù)據(jù)庫中一共有多少條數(shù)據(jù)
$Total = $total[0]; //
$Totalpage = ceil($Total/$perpagenum);//上舍,取整
if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage)//page可能的四種狀態(tài)
{
$page=1;
}
else
{
$page=$_GET['page'];//如果不滿足以上四種情況,則page的值為$_GET['page']
}
$startnum = ($page-1)*$perpagenum;//開始條數(shù)
$sql = "select * from a order by id limit $startnum,$perpagenum";//查詢出所需要的條數(shù)
echo $sql."
";
$rs = mysql_query($sql);
$contents = mysql_fetch_array($rs);
if($total)如果$total不為空則執(zhí)行以下語句
{
do
{
$id = $contents['id'];
$name = $contents['name'];
?>
<table border="0" align="center">
<tr>
<td>id:
<?php echo $id;?>
</td>
</tr>
<tr>
<td>name:
<?php echo $name;?>
</td>
</tr>
</table>
<?php
}
while($contents = mysql_fetch_array($rs));//do....while
$per = $page - 1;//上一頁
$next = $page + 1;//下一頁
echo "<center>共有".$Total."條記錄,每頁".$perpagenum."條,共".$Totalpage."頁 ";
if($page != 1)
{
echo "<a href='".$_SERVER['PHP_SELF']."'>首頁</a>";
echo "<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></center>";
}
}
else如果$total為空則輸出No message
{
echo "<center>No message</center>";
}
?>
相關(guān)文章
在PHP中實(shí)現(xiàn)使用Guzzle執(zhí)行POST和GET請求
今天小編就為大家分享一篇在PHP中實(shí)現(xiàn)使用Guzzle執(zhí)行POST和GET請求,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10Yii2.0表關(guān)聯(lián)查詢實(shí)例分析
這篇文章主要介紹了Yii2.0表關(guān)聯(lián)查詢的方法,結(jié)合實(shí)例形式分析了Yii中關(guān)聯(lián)查詢的實(shí)現(xiàn)方法與相關(guān)使用技巧,需要的朋友可以參考下2016-07-07在Laravel的Model層做數(shù)據(jù)緩存的實(shí)現(xiàn)
這篇文章主要介紹了在Laravel的Model層做數(shù)據(jù)緩存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09php示例詳解Constructor Prototype Pattern 原型模式
原型模式是一種創(chuàng)建者模式,其特點(diǎn)在于通過“復(fù)制”一個已經(jīng)存在的實(shí)例來返回新的實(shí)例,而不是新建實(shí)例。2015-10-10Zend Framework教程之請求對象的封裝Zend_Controller_Request實(shí)例詳解
這篇文章主要介紹了Zend Framework教程之請求對象的封裝Zend_Controller_Request用法,結(jié)合實(shí)例形式詳細(xì)分析了請求對象封裝的原理,使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03