本文實例講述了php數(shù)組分頁實現(xiàn)方法。分享給大家供大家參考,具體如下:
<?php
$arr_click = array(
array( 'clicks' => 3, 'clickDate' =>'2010-10-11' ),
array( 'clicks' => 2, 'clickDate' =>'2010-10-10' ),
array( 'clicks' => 3, 'clickDate' =>'2010-10-09' ),
array( 'clicks' => 4, 'clickDate' =>'2010-10-08' ),
array( 'clicks' => 5, 'clickDate' =>'2010-10-13' ),
array( 'clicks' => 7, 'clickDate' =>'2010-10-14' ),
array( 'clicks' => 6, 'clickDate' =>'2010-10-15' ),
array( 'clicks' => 9, 'clickDate' =>'2010-10-16' ),
);
if(!isset($_GET['page']))
{
$page = 1;
}
else
{
$page=$_GET['page'];
}
$size=3;//每頁顯示的記錄數(shù)
$pnum = ceil(count($arr_click) / $size); //總頁數(shù),ceil()函數(shù)用于求大于數(shù)字的最小整數(shù)
//用array_slice(array,offset,length) 函數(shù)在數(shù)組中根據(jù)條件取出一段值;array(數(shù)組),offset(元素的開始位置),length(組的長度)
$newarr = array_slice($arr_click, ($page-1)*$size, $size);
for($i=0;$i<count($newarr);$i++)
{
echo $newarr[$i]['clickDate']."<br/>";
}
?>
<?php
if(!isset($_GET['page']) || $_GET['page']<=1){
?>
<a href="11111111111111.php?page=1">上一頁</a>
<?php }else{ ?>
<a href="11111111111111.php?page=<?php echo $page-1;?>">上一頁</a>
<?php } ?>
<?php if($_GET['page']>=$pnum) {?>
<a href="11111111111111.php?page=<?php echo $pnum;?>">下一頁</a>
<?php }else{ ?>
<a href="11111111111111.php?page=<?php echo $page+1;?>">下一頁</a>
<?php } ?>
希望本文所述對大家PHP程序設計有所幫助。