当前位置:首页 > PHP教程 > php数组 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2018-07-31 11:30:03 浏览: 评论:0 
  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.  $linkStyle=''
  50.  switch($number){ 
  51.   case1: 
  52.   $linkStyle="<a href="\"?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.   $linkStyle="<a href="\"?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. header('Content-Type: text/html;charset=utf-8'); 
  63. $array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20"); 
  64. $page= isset($_GET['page'])? $_GET['page'] : 1 ; 
  65. $arrayPage = new PaginationArray($array,"5",$page); 
  66. $r = $arrayPage->page(); 
  67. foreach($r["source"] as $s){ 
  68.  echo $s.'<br>'; 
  69. } 
  70. echo $r["links"]; 
  71. */ 
  72. ?> 

Tags: 组分 对数 实例

分享到: