日期:2013-05-30 浏览次数:20482 次
<?
//分页函数
function genpage(&$sql,$page_size=20)
{
global $prepage,$nextpage,$pages,$sums; //out param
$page = $_GET["page"]>=1?$_GET["page"]:1;
$eachpage = $page_size;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$result = mysql_query($pagesql);
if($rs = mysql_fetch_array($result)) $sums = $rs[0];
$pages = ceil(($sums-0.5)/$eachpage)-1;
$pages = $pages>=0?$pages:0;
$prepage = ($page>0)?$page-1:0;
$nextpage = ($page<$pages)?$page+1:$pages;
$startpos = $page*$eachpage;
$sql .=" limit $startpos,$eachpage ";
}
//显示分页
function showpage()
{
global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
echo "共".($pages+1)."页: ";
if($page>0)echo "<a href=$PHP_SELF?page=0&$queryString>第一页</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."&$queryString>«</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?page=$i&$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."&$queryString>»</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?page=$pages&$queryString>最后页</a>";
}
?>
--------------------------------------------------------------------------------
调用:
PHP代码:--------------------------------------------------------------------------------