像 google 一样的分页算法

<?php
function page ($AllPages,$PageNo,$url,$halfPer){
    $re='';
    $re .= ( $PageNo > 1 )?"<li><a href=\"$url=1\" title='首页'><<</a></li>"  : '';
    for ( $i = $PageNo - $halfPer,$i >= 1 || $i = 1 ,$j = $PageNo + $halfPer, $j < $AllPages || $j = $AllPages;$i <= $j ;$i++ )
 {//核心算法!
        $re .= ($i == $PageNo)
            ? "<li class='currentPage'> $i</li>"
            : "<li><a href=\"$url=$i\">$i</a></li>";
    }
 $re .= ($PageNo < $AllPages )?"<li><a href=\"$url=".($AllPages)."\" title='末页'>>></a></li>":'';
    $re="<ul id='nav'>$re</ul>";
    echo $re;
}

$AllNum   = 205;//此为您检索出来的总的记录数
$PageSize = 15;
$AllPages = ceil($AllNum/$PageSize);
$PageNo   = $_GET['page'];
if(!is_numeric($PageNo) && $PageNo<1)$PageNo=1;
if($PageNo>$AllPages)$PageNo=$AllPages;
$url      = "?page";
$halfPer  = 5;
?>

<style type="text/css">
<!--
body,html{font-size:12px;}
#nav{list-style:none; font-size:14px;}
#nav a,#nav .currentPage
{font-size:14px; text-align:center; text-decoration:none; width:20px; height:20px; line-height:20px;}
#nav li{margin-right:5px;float:left;}
#nav a {color:#F25500;display:block;border:1px solid #CACED1;}
#nav li a:hover,#nav .currentPage
{color: #FFFFFF;border:1px solid #F25500;background:#F25500;}

-->
</style>
总页数:<?=$AllPages ?> &nbsp;当前页:<?=$PageNo ?>
<?php page($AllPages,$PageNo,$url,$halfPer); ?>

posted @ 2008-09-26 15:12  幽暗之灵  阅读(1238)  评论(0)    收藏  举报

零度科技