這里給大家分享的是一段php使用array()函數(shù)實(shí)現(xiàn)分頁(yè)的代碼,算是一種比較另類的分頁(yè)思路,這里推薦給大家。
代碼很簡(jiǎn)單,就不多廢話了。
代碼如下:
$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");//規(guī)定要分頁(yè)的數(shù)組
$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);//求總頁(yè)數(shù)
$prev=(($current-1)<=0 ? "1":($current-1));//確定上一頁(yè),如果當(dāng)前頁(yè)是第一頁(yè),點(diǎn)擊顯示第一頁(yè)
$next=(($current+1)>=$total ? $total:$current+1);//確定下一頁(yè),如果當(dāng)前頁(yè)是最后一頁(yè),點(diǎn)擊下頁(yè)顯示最后一頁(yè)
$current=($current>($total)?($total):$current);//當(dāng)前頁(yè)如果大于總頁(yè)數(shù),當(dāng)前頁(yè)為最后一頁(yè)
$start=($current-1)*$pagesize;//分頁(yè)顯示時(shí),應(yīng)該從多少條信息開始讀取
for($i=$start;$i<($start+$pagesize);$i++){
array_push($_return,$array[$i]);//將該顯示的信息放入數(shù)組 $_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>";//分頁(yè)樣式可跟據(jù)需要調(diào)整
return $pagearray;
}
以上就是使用array函數(shù)實(shí)現(xiàn)分頁(yè)的核心代碼了,希望大家能夠喜歡。
更多信息請(qǐng)查看IT技術(shù)專欄