PHP实现对数组分页处理实例详解
作者:风起从容 时间:2023-11-21 07:08:13
本文实例讲述了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"];
*/
?>
希望本文所述对大家PHP程序设计有所帮助。
标签:PHP,数组,分页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python使用pytest接口自动化测试的使用
2022-06-15 05:23:48
![](https://img.aspxhome.com/file/2023/7/125207_0s.jpg)
Django扫码抽奖平台的配置过程详解
2021-11-25 22:54:09
![](https://img.aspxhome.com/file/2023/2/112492_0s.png)
Python全栈之线程详解
2021-05-21 17:44:21
![](https://img.aspxhome.com/file/2023/4/101224_0s.png)
Python模块相关知识点小结
2022-04-12 06:58:23
![](https://img.aspxhome.com/file/2023/5/102905_0s.png)
Python利用scapy实现ARP欺骗的方法
2021-07-24 08:26:03
![](https://img.aspxhome.com/file/2023/4/133674_0s.jpg)
python 实现体质指数BMI计算
2023-01-25 12:44:56
![](https://img.aspxhome.com/file/2023/1/110571_0s.png)
Python 实现文件读写、坐标寻址、查找替换功能
2021-06-02 07:36:42
![](https://img.aspxhome.com/file/2023/4/127014_0s.jpg)
简单了解django文件下载方式
2022-10-31 08:48:32
CentOS 7下安装与配置MySQL 5.7
2024-01-26 17:41:50
![](https://img.aspxhome.com/file/2023/1/70381_0s.png)
Layui 数据表格批量删除和多条件搜索的实例
2024-04-10 10:43:56
使用python批量转换文件编码为UTF-8的实现
2023-03-07 03:19:41
Flask实现跨域请求的处理方法
2022-03-18 08:19:07
pytorch交叉熵损失函数的weight参数的使用
2021-02-27 15:52:31
![](https://img.aspxhome.com/file/2023/3/101703_0s.jpg)
关于tensorflow的几种参数初始化方法小结
2023-10-15 12:05:26
keras实现调用自己训练的模型,并去掉全连接层
2023-08-10 16:34:21
django项目环境搭建及在虚拟机本地创建django项目的教程
2022-10-14 14:04:32
![](https://img.aspxhome.com/file/2023/6/102806_0s.png)
Quester解读17条广告效果测定
2007-11-27 12:51:00
python实现LRU热点缓存及原理
2022-01-08 01:26:15
asp如何验证信用卡是否可用?
2010-06-10 18:39:00
php获取客户端IP及URL的方法示例
2024-05-03 15:49:01