代碼很簡單,就不多廢話了。
復制代碼 代碼如下:
$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");//規定要分頁的數組
$page= $_GET['page'];
$r = $this->page($array,5,$page);
print_r($r);
exit();
foreach($r["source"] as $s){
echo $s;
}
function page($array,$pagesize,$current){
$_return=array();
$total=ceil(Count($array)/$pagesize);//求總頁數
$prev=(($current-1)<=0 ? "1":($current-1));//確定上一頁,如果當前頁是第一頁,點擊顯示第一頁
$next=(($current+1)>=$total ? $total:$current+1);//確定下一頁,如果當前頁是最後一頁,點擊下頁顯示最後一頁
$current=($current>($total)?($total):$current);//當前頁如果大於總頁數,當前頁為最後一頁
$start=($current-1)*$pagesize;//分頁顯示時,應該從多少條信息開始讀取
for($i=$start;$i<($start+$pagesize);$i++){
array_push($_return,$array[$i]);//將該顯示的信息放入數組 $_return 中
}
$pagearray["source"]=$_return;
$pagearray["page"]="<a href=\"?page=1\">first</a> <a href=\"?page={$prev}
\">prev</a> <a href=\"?page={$next}\">next</a> <a href=\"?page={$total}\">end</a>";//分頁樣式可跟據需要調整
return $pagearray;
}
以上就是使用array函數實現分頁的核心代碼了,希望大家能夠喜歡。