• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP实现对数组分页处理的方法

php 搞代码 3年前 (2022-01-22) 13次浏览 已收录 0个评论

这篇文章主要介绍了PHP实现对数组分页处理,结合实例形式分析了php封装的数组分页类定义与使用技巧,需要的朋友可以参考下

具体如下:

最近用到了用数组数据分页,这里整理了一下,具体代码如下:

<?phpclass 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}\"&g<i style="color:transparent">本#文来源gaodai$ma#com搞$$代**码网$</i><button>搞代gaodaima码</button>t;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"];*/?>

以上就是本文的全部内容,希望对大家的学习有所帮助。

相关推荐:

php+redis实现注册、删除、编辑、分页、登录、关注等功能的方法

Ajax无刷新分页的性能优化方法

php简单实现分页查询的方法

以上就是PHP实现对数组分页处理的方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP实现对数组分页处理的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址