PHP中对数组进行分页处理的原理及分页实例

PHP中对数组进行分页处理的原理及分页实例  

最近用到了用数组数据分页,所以就整理了一下,希望能对大家有用!

<?php


class PaginationArray{
 
 public $pageArray=array(); //数组
 public $pageSize=10; //每页显示记录数
 public $current= 1; //当前页
 
 private $total=0; //总页数
 private $prev=0; //上一页
 private $next=0; //下一页
 
 public $argumetsOther='';//设置参数
 
 function __construct($array=array(),$pageSize=10,$current=1){
  
  $this->pageArray=$array;
  $this->pageSize=$pageSize;
  $this->current=$current; 
 }
 
 /*通过数组进行初始化
  * 
  * 数组为关联数组,参数索引为pageArray,pageSize,current
  * 
  */
 function setArguments($arr){
  if (is_array($arr)){
   $this->pageArray=$arr['pageArray'];
   $this->pageSize=$arr['pageSize'];
   $this->current=$arr['current'];
  }else{
   return ;
  }
 }
 
 //返回链接
 function page(){
  $_return=array();
  /*calculator*/
  $this->total=ceil(Count($this->pageArray)/$this->pageSize);
  $this->prev=(($this->current-1)<= 0 ? "1":($this->current-1));
  $this->next=(($this->current+1)>=$this->total ? $this->total:$this->current+1);
  
  $current=($this->current>($this->total)?($this->total):$this->current);
  
  $start=($this->current-1)*$this->pageSize;
  $arrleng=count($this->pageArray);
  
  for($i=$start;$i<($start+$this->pageSize);$i++){
   if($i >= $arrleng)break;
   array_push($_return,$this->pageArray[$i]);
  }
  $pagearray["source"]=$_return;
  $pagearray["links"]=$this->linkStyle(2);
  return $pagearray;
 }
 
 
 //链接的样式
 private function linkStyle($number=1){
  $linkStyle='';
  switch ($number){
   case 1:
    $linkStyle="<a href=\"?page=1\">first</a> <a href=\"?page={$this->prev}\">prev</a> <a href=\"?page={$this->next}\">next</a> <a href=\"?page={$this->total}\">end</a>";
    break;
   case 2:
    $linkStyle="<a href=\"?page=1&{$this->argumetsOther}\">首页</a> <a href=\"?page={$this->prev}&{$this->argumetsOther}\">上一页</a> <a href=\"?page={$this->next}&{$this->argumetsOther}\">下一页</a> <a href=\"?page={$this->total}&{$this->argumetsOther}\">尾页</a>";
    break;
  }
  return $linkStyle;
 }
 
}


//调用的实例
/*
header('Content-Type: text/html;charset=utf-8');
$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
$page=  isset($_GET['page'])? $_GET['page'] : 1 ;

$arrayPage = new PaginationArray($array,"5",$page);
$r = $arrayPage->page();
foreach($r["source"] as $s){
 echo $s.'<br />';
}

echo $r["links"];

*/


?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_72429728

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值