PHP实现对数组分页处理实例详解

  1. <?php
  2. classPaginationArray{
  3. public$pageArray=array();//数组
  4. public$pageSize=10;//每页显示记录数
  5. public$current= 1;//当前页
  6. private$total=0;//总页数
  7. private$prev=0;//上一页
  8. private$next=0;//下一页
  9. public$argumetsOther='';//设置参数
  10. function__construct($array=array(),$pageSize=10,$current=1){
  11. $this->pageArray=$array;
  12. $this->pageSize=$pageSize;
  13. $this->current=$current;
  14. }
  15. /*通过数组进行初始化
  16. *
  17. * 数组为关联数组,参数索引为pageArray,pageSize,current
  18. *
  19. */
  20. functionsetArguments($arr){
  21. if(is_array($arr)){
  22. $this->pageArray=$arr['pageArray'];
  23. $this->pageSize=$arr['pageSize'];
  24. $this->current=$arr['current'];
  25. }else{
  26. return;
  27. }
  28. }
  29. //返回链接
  30. functionpage(){
  31. $_return=array();
  32. /*calculator*/
  33. $this->total=ceil(Count($this->pageArray)/$this->pageSize);
  34. $this->prev=(($this->current-1)<= 0="" this-="">current-1));<!--=-->
  35. $this->next=(($this->current+1)>=$this->total ?$this->total:$this->current+1);
  36. $current=($this->current>($this->total)?($this->total):$this->current);
  37. $start=($this->current-1)*$this->pageSize;
  38. $arrleng=count($this->pageArray);
  39. for($i=$start;$i<($start+$this->pageSize);$i++){<!--($start+$this--->
  40. if($i>=$arrleng)break;
  41. array_push($_return,$this->pageArray[$i]);
  42. }
  43. $pagearray["source"]=$_return;
  44. $pagearray["links"]=$this->linkStyle(2);
  45. return$pagearray;
  46. }
  47. //链接的样式
  48. privatefunctionlinkStyle($number=1){
  49. $link;
  50. switch($number){
  51. case1:
  52. $link\"?page=1\"">first</a> <a href="\"?page={$this-">prev</a> <a href="\"?page={$this-">next</a> <a href="\"?page={$this-">end</a>";
  53. break;
  54. case2:
  55. $link\"?page=1&{$this-">首页</a> <a href="\"?page={$this-">上一页</a> <a href="\"?page={$this-">下一页</a> <a href="\"?page={$this-">尾页</a>";
  56. break;
  57. }
  58. return$linkStyle;
  59. }
  60. }
  61. //调用的实例
  62. /*
  63. header('Content-Type: text/html;charset=utf-8');
  64. $array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
  65. $page= isset($_GET['page'])? $_GET['page'] : 1 ;
  66. $arrayPage = new PaginationArray($array,"5",$page);
  67. $r = $arrayPage->page();
  68. foreach($r["source"] as $s){
  69. echo $s.'<br>';
  70. }
  71. echo $r["links"];
  72. */
  73. ?>