複製代碼 代碼如下:<?php
function genpage(&$sql,$page_size=10)
{
global $pages,$sums,$eachpage,$page; //總頁數,總記錄,每頁數,當前頁
$page = $_GET["page"];
if($page ==0)$page =1;
$eachpage = $page_size;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$conn = mysql_query($pagesql) or die(mysql_error());
if($rs = mysql_fetch_array($conn))$sums = $rs[0];
$pages=ceil($sums/$eachpage);
if($pages==0)$pages=1;
$startpos = ($page-1)*$eachpage;
$sql .=" limit $startpos,$eachpage ";
}
//顯示分頁
function showpage()
{
global $pages,$sums,$eachpage,$page; //總頁數,總記錄,每頁數,當前頁,其它參數
$link=$_SERVER['PHP_SELF'];
echo "記錄".$sums.":".$eachpage." ";
echo "頁數".$page."/".$pages." ";
$p_head=$page-5;
if($p_head<=0)$p_head=1; //頁碼迴圈開始數 前5個
$p_end=$page+5;
if($p_end>$pages)$p_end=$pages; //頁碼迴圈結束數 後5個
echo "[<a href=$link?page=1>首頁</a>] ";
for($i=$p_head;$i<=$p_end;$i++)
{
if($i!=$page)
echo "<a href=$link?page=$i>[$i]</a> ";
else
echo "<b><strike>[$i]</strike></b> ";
}
echo " [<a href=$link?page=$pages>末頁</a>]";
}
?>