本文实例讲述了PHP实现对数组分页处理方法。分享给大家供大家参考,具体如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<?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程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 71
-
2025-05-29 22
-
2025-05-25 34
-
2025-05-25 81
-
2025-05-27 85
热门评论