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 rel="nofollow noopener noreferrer" href=\"?page=1\">first</a> <a rel="nofollow noopener noreferrer" href=\"?page={$this->prev}\">prev</a> <a rel="nofollow noopener noreferrer" href=\"?page={$this->next}\">next</a> <a rel="nofollow noopener noreferrer" href=\"?page={$this->total}\">end</a>";
  break;
  case 2:
  $linkStyle="<a rel="nofollow noopener noreferrer" href=\"?page=1&{$this->argumetsOther}\">首页</a> <a rel="nofollow noopener noreferrer" href=\"?page={$this->prev}&{$this->argumetsOther}\">上一页</a> <a rel="nofollow noopener noreferrer" href=\"?page={$this->next}&{$this->argumetsOther}\">下一页</a> <a rel="nofollow noopener noreferrer" 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"];
*/
?>

希望本文所述对大家PHP程序设计有所帮助。

本文PHP实现对数组分页处理实例详解到此结束。树木长得高是为了吸收阳光,老鹰飞得高是为了捕捉猎物,人站得高是为了接受挑战。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
PHP中的数组分页实现(非数据库)实例讲解

ThinkPHP5分页paginate代码实例解析

THINKPHP5分页数据对象处理过程解析

TP3.2框架分页相关实现方法分析

Laravel5.1 框架分页展示实现方法实例分析