php数组分页实现方法

本文实例讲述了php数组分页实现方法。分享给大家供大家参考,具体如下:

  1. <?php
  2. $arr_click = array(
  3. array( 'clicks' => 3, 'clickDate' =>'2010-10-11' ),
  4. array( 'clicks' => 2, 'clickDate' =>'2010-10-10' ),
  5. array( 'clicks' => 3, 'clickDate' =>'2010-10-09' ),
  6. array( 'clicks' => 4, 'clickDate' =>'2010-10-08' ),
  7. array( 'clicks' => 5, 'clickDate' =>'2010-10-13' ),
  8. array( 'clicks' => 7, 'clickDate' =>'2010-10-14' ),
  9. array( 'clicks' => 6, 'clickDate' =>'2010-10-15' ),
  10. array( 'clicks' => 9, 'clickDate' =>'2010-10-16' ),
  11. );
  12. if(!isset($_GET['page']))
  13. {
  14. $page = 1;
  15. }
  16. else
  17. {
  18. $page=$_GET['page'];
  19. }
  20. $size=3;//每页显示的记录数
  21. $pnum = ceil(count($arr_click) / $size); //总页数,ceil()函数用于求大于数字的最小整数
  22. //用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
  23. $newarr = array_slice($arr_click, ($page-1)*$size, $size);
  24. for($i=0;$i<count($newarr);$i++) {="" echo="" $newarr[$i]['clickdate']."<br="">";
  25. }
  26. ?>
  27. <?php
  28. if(!isset($_GET['page']) || $_GET['page']<=1){
  29. ?>
  30. <a href="11111111111111.php?page=1">上一页</a>
  31. <?php }else{ ?>
  32. <a href="11111111111111.php?page=<?php echo $page-1;?>">上一页</a>
  33. <?php } ?>
  34. <?php if($_GET['page']>=$pnum) {?>
  35. <a href="11111111111111.php?page=<?php echo $pnum;?>">下一页</a>
  36. <?php }else{ ?>
  37. <a href="11111111111111.php?page=<?php echo $page+1;?>">下一页</a>
  38. <?php } ?>
  39. //phpfensi.com
  40. </count($newarr);$i++)>