一個簡單的php分頁小模塊
<?php
$str=pagestr("http:www.abc.com?",195,11,10,8);
print($str);
//url連接全度,countdatas表數(shù)據(jù)總行數(shù)妖泄,currentpage第幾頁(0是第一頁)坐漏,everpage每頁多少行,beforlaterln前后長度
function pagestr($url,$countdatas,$currentpage=0,$everpage=10,$beforlaterln=5){
$page="";
$intpart=$countdatas/$everpage;
$remainderpart=$countdatas%$everpage;//余數(shù)
$countpage=floor($intpart);//取整數(shù)
$currentpage = $currentpage<0 ? 0:$currentpage;
if($remainderpart==0){//余數(shù)判斷
$currentpage=$currentpage>$countpage-1?$countpage-1:$currentpage;
$countpage;//總頁數(shù)
$pp=$currentpage+1;
$strbe="";
$pagebefore=$pp-$beforlaterln-1;
for($i=$pp; $i > $pagebefore; $i--){
if($i>0 && $i<$pp){
$strbe="<a class='num' href='".$url."&p=".($i-1)."'>".$i."</a>".$strbe;
}
}
if($pp<=1){$strbe="";}else{$strbe="<a class='num' href='".$url."&p=0'>首頁</a>"."<a class='num' href='".$url."&p=".($pp-2)."'>上一頁</a>".$strbe;}
$strmid="<span class='current'>".$pp."</span>";
$strlater="";
$pagelater=$pp+$beforlaterln;
for($i=$pp; $i < $pagelater; $i++){
if($i<$countpage){
$strlater.="<a class='num' href='".$url."&p=".($i)."'>".($i+1)."</a>";
}
}
if($pp>=$countpage){}else{$strlater.="<a class='num' href='".$url."&p=".$pp."'>下一頁</a>"."<a class='num' href='".$url."&p=".($countpage-1)."'>尾頁</a>";}
$page=$strbe.$strmid.$strlater;
}else{
$countpage+=1;//總頁數(shù)
$currentpage=$currentpage>$countpage-1?$countpage-1:$currentpage;
$pp=$currentpage+1;
$strbe="";
$pagebefore=$pp-$beforlaterln-1;
for($i=$pp; $i > $pagebefore; $i--){
if($i>0 && $i<$pp){
$strbe="<a class='num' href='".$url."&p=".($i-1)."'>".$i."</a>".$strbe;
}
}
if($pp<=1){$strbe="";}else{$strbe="<a class='num' href='".$url."&p=0'>首頁</a>"."<a class='num' href='".$url."&p=".($pp-2)."'>上一頁</a>".$strbe;}
$strmid="<span class='current'>".$pp."</span>";
$strlater="";
$pagelater=$pp+$beforlaterln;
for($i=$pp; $i < $pagelater; $i++){
if($i<$countpage){
$strlater.="<a class='num' href='".$url."&p=".($i)."'>".($i+1)."</a>";
}
}
if($pp>=$countpage){}else{$strlater.="<a class='num' href='".$url."&p=".$pp."'>下一頁</a>"."<a class='num' href='".$url."&p=".($countpage-1)."'>尾頁</a>";}
$page=$strbe.$strmid.$strlater;
}
return "共".$countpage."頁".$page;
}
?>
運行后得到一下字符串
共20頁<a class='num' >首頁</a>
<a class='num' >上一頁</a>
<a class='num' >4</a>
<a class='num' >5</a>
<a class='num' >6</a>
<a class='num' >7</a>
<a class='num' >8</a>
<a class='num' >9</a>
<a class='num' >10</a>
<a class='num' >11</a>
<span class='current'>12</span>
<a class='num' >13</a>
<a class='num' >14</a>
<a class='num' >15</a>
<a class='num' >16</a>
<a class='num' >17</a>
<a class='num' >18</a>
<a class='num' >19</a>
<a class='num' >20</a>
<a class='num' >下一頁</a>
<a class='num' >尾頁</a>
希望對大家有幫助,謝謝!