测试图例:
效果图:
实现代码:
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/**
*
* @param $page 页码(1至正无穷)
* @param $num 数据中多上行为一页
* @param $rows 数据的总行数
* @param $length 下标的最大长度
* @return array
*/
public function PageDate( $page , $num , $rows , $length ){
//初始化数据
$MaxPage = 0; //最大页码
$MinPage = 0; //最小页码
$ServerPage = 0; //下拉框中显示的页码
$PageData = array (); //全部的页码集合(用户下拉框选择)
$PageShowData = array (); //用于显示下标的页码集合
$page = intval ( $page );
if ( $rows == 0){
return $data = array (
'page' => $page ,
'MaxPage' => $MaxPage ,
'MinPage' => $MinPage ,
'ServerPage' => $ServerPage ,
'PageData' => $PageData ,
'PageShowData' => $PageShowData
);
}
//得到最大分页和最小分页
$MaxPage = intval ( $rows / $num );
$double = $rows % $num ;
if ( $double > 0.000000001)
{
$MaxPage += 1;
}
$MinPage = 1;
$ServerPage = 1;
//得到页码的全部项目
for ( $i = 1; $i <= $MaxPage ; $i ++ ){
$PageData [ $i ] = $i ;
}
//对页码进行正确性改正
if ( $page < $MinPage ){
$page = 1;
}
if ( $page > $MaxPage ){
$page = 1;
}
/*生成显示的页码标签*/
//生成左侧的代码快
if ( $page == $MinPage ){
$PageShowData [1] = $page ;
}
if ( $page > $MinPage ){
$PageShowData [1] = $MinPage ;
// $PageShowData[2] = $page - 1; // <<
$PageShowData [2] = '<<' ; // <<
if ( $page -2 > $MinPage ){
$PageShowData [3] = $page - 2;
$PageShowData [4] = $page - 1;
} elseif ( $page -2 == $MinPage ){
$PageShowData [3] = $page - 1;
}
//确定选中的页码代码快
$PageShowData [ count ( $PageShowData ) + 1] = $page ;
}
$COUNT = count ( $PageShowData ) + 1;
//生成右侧的代码块
if ( $page == $MaxPage ){
return $data = array (
'page' => $page ,
'MaxPage' => $MaxPage ,
'MinPage' => $MinPage ,
'ServerPage' => $ServerPage ,
'PageData' => $PageData ,
'PageShowData' => $PageShowData
);
}
if ( $page < $MaxPage ) {
if ( $MaxPage - $page >= 2){
$j = 1;
for ( $i = $COUNT ; $i <= $length ; $i ++){
$PageShowData [ $i ] = $page + $j ;
$j ++;
if ( $PageShowData [ $i ] == $MaxPage -1){
break ;
}
}
}
$COUNT = count ( $PageShowData ) + 1;
// $PageShowData[$COUNT] = $page + 1; //>>
$PageShowData [ $COUNT ] = '>>' ;
$PageShowData [ $COUNT + 1] = $MaxPage ;
//得到选中的参数
$ServerPage = $PageShowData [ $COUNT -1] + 1;
if ( $ServerPage > $MaxPage ){
$ServerPage = 1;
}
return $data = array (
'page' => $page ,
'MaxPage' => $MaxPage ,
'MinPage' => $MinPage ,
'ServerPage' => $ServerPage ,
'PageData' => $PageData ,
'PageShowData' => $PageShowData
);
}
}
|
本文出自 IT985博客