<?php
class
Page_Link
{
var
$page_max
= 10;
var
$page_num
= 10;
var
$length
= 20;
var
$isNextPage
= true;
var
$isFirstPage
= false;
function
Calculation_Page_Num(
$total
)
{
$this
->page_num =
ceil
(
$total
/
$this
->length );
return
$this
->page_num;
}
function
Calculation_Min_Max(
$act_page
= 1 )
{
$py_left
= 0;
$py_right
= 0;
$bj_left
= 0;
$bj_right
= 0;
$gd_left
= 0;
$gd_right
= 0;
if
( (
$this
->page_num -
$this
->page_max ) <= 0 )
{
$bj_left
= 1;
$bj_right
=
$this
->page_num;
}
else
{
$tmp
=
$this
->page_max % 2;
if
(
$tmp
=== 1 )
{
$py_left
=
$py_right
= (
$this
->page_max - 1 ) / 2;
}
else
{
$py_left
=
$this
->page_max / 2 - 1;
$py_right
=
$this
->page_max / 2;
}
$gd_left
= 1 +
$py_left
;
$gd_right
=
$this
->page_num -
$py_right
;
if
(
$act_page
>=
$gd_left
&&
$act_page
<=
$gd_right
)
{
$bj_left
=
$act_page
-
$py_left
;
$bj_right
=
$act_page
+
$py_right
;
}
else
{
if
( (
$act_page
-
$py_left
) <= 1 )
{
$bj_left
= 1;
$bj_right
=
$this
->page_max;
}
else
{
$bj_left
=
$this
->page_num -
$this
->page_max + 1;
$bj_right
=
$this
->page_num;
}
}
}
$res
=
array
();
$res
['min'] =
$bj_left
;
$res
['max'] =
$bj_right
;
return
$res
;
}
function
make_page(
$total
,
$act_page
,
$url
,
$param
)
{
$page_num
=
$this
->Calculation_Page_Num(
$total
);
$arr_min_max
=
$this
->Calculation_Min_Max(
$act_page
);
if
(!
eregi
(
"([?|&]$param=)"
,
$url
)) {
$url
=
strpos
(
$url
,
"?"
)===false?
$url
.
"?"
:
$url
.
"&"
;
$url
=
$url
.
"$param=0"
;
}
if
(
$act_page
>
$page_num
)
{
$act_page
=
$page_num
;
}
$url
=
eregi_replace
(
$param
. '=[0-9]+',
$param
. '=0',
$url
);
$res
=
array
();
$d
= 0;
for
(
$i
=
$arr_min_max
['min'];
$i
<=
$arr_min_max
['max'];
$i
++ )
{
if
(
$i
==
$act_page
)
{
$res
[
$d
]['url'] = '';
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
else
{
$res
[
$d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$i
,
$url
);
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
$d
++;
}
if
(
$this
->isNextPage )
{
$res
=
$this
->make_before_next_link(
$res
,
$act_page
,
$url
,
$param
);
}
if
(
$this
->isFirstPage )
{
$res
=
$this
->make_first_end_link(
$res
,
$act_page
,
$url
,
$param
);
}
return
$res
;
}
function
make_page_with_total(
$total
,
$act_page
,
$url
,
$param
)
{
$page_num
=
$this
->Calculation_Page_Num(
$total
);
$arr_min_max
=
$this
->Calculation_Min_Max(
$act_page
);
if
(!
eregi
(
"([?|&]$param=)"
,
$url
)) {
$url
=
strpos
(
$url
,
"?"
)===false?
$url
.
"?"
:
$url
.
"&"
;
$url
=
$url
.
"$param=0"
;
}
if
(
$act_page
>
$page_num
)
{
$act_page
=
$page_num
;
}
$url
=
eregi_replace
(
$param
. '=[0-9]+',
$param
. '=0',
$url
);
$res
=
array
();
$d
= 0;
for
(
$i
=
$arr_min_max
['min'];
$i
<=
$arr_min_max
['max'];
$i
++ )
{
if
(
$i
==
$act_page
)
{
$res
[
$d
]['url'] = '';
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
else
{
$res
[
$d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$i
,
$url
);
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
$d
++;
}
if
(
$this
->isNextPage )
{
$res
=
$this
->make_before_next_link(
$res
,
$act_page
,
$url
,
$param
);
}
if
(
$this
->isFirstPage )
{
$res
=
$this
->make_first_end_link(
$res
,
$act_page
,
$url
,
$param
);
}
$total_num
=
ceil
(
$total
/
$this
->length);
$result
['total']=
$total_num
;
$result
['DATA']=
$res
;
return
$result
;
}
function
make_before_next_link(
$arr
,
$act
,
$url
,
$param
)
{
$tmp
=
array
();
$before
=
$act
- 1;
$next
=
$act
+ 1;
if
(
$before
< 1 )
{
$before
= 1;
$tmp
[0]['url'] = '';
$tmp
[0]['name'] =
"上一页"
;
$tmp
[0]['no'] =
$before
;
}
else
{
$tmp
[0]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$before
,
$url
);
$tmp
[0]['name'] =
"上一页"
;
$tmp
[0]['no'] =
$before
;
}
$counts
= sizeof(
$arr
);
$tmp_count
= sizeof(
$tmp
);
for
(
$i
= 0;
$i
<
$counts
;
$i
++ )
{
$tmp
[
$tmp_count
]['url'] =
$arr
[
$i
]['url'];
$tmp
[
$tmp_count
]['name'] =
$arr
[
$i
]['name'];
$tmp
[
$tmp_count
]['no'] =
$arr
[
$i
]['no'];
$tmp_count
++;
}
if
(
$next
>
$this
->page_num )
{
$next
=
$this
->page_num;
$tmp
[
$tmp_count
]['url'] = '';
$tmp
[
$tmp_count
]['name'] =
"下一页"
;
$tmp
[
$tmp_count
]['no'] =
$next
;
}
else
{
$tmp
[
$tmp_count
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$next
,
$url
);
$tmp
[
$tmp_count
]['name'] =
"下一页"
;
$tmp
[
$tmp_count
]['no'] =
$next
;
}
return
$tmp
;
}
function
make_first_end_link(
$arr
,
$act
,
$url
,
$param
)
{
$tmp
=
array
();
$before
= 1;
$next
=
$this
->page_num;
if
(
$act
== 1 )
{
$before
= 1;
$tmp
[0]['url'] = '';
$tmp
[0]['name'] =
"首页"
;
$tmp
[0]['no'] =
$before
;
}
else
{
$tmp
[0]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$before
,
$url
);
$tmp
[0]['name'] =
"首页"
;
$tmp
[0]['no'] =
$before
;
}
$counts
= sizeof(
$arr
);
$tmp_count
= sizeof(
$tmp
);
for
(
$i
= 0;
$i
<
$counts
;
$i
++ )
{
$tmp
[
$tmp_count
]['url'] =
$arr
[
$i
]['url'];
$tmp
[
$tmp_count
]['name'] =
$arr
[
$i
]['name'];
$tmp
[
$tmp_count
]['no'] =
$arr
[
$i
]['no'];
$tmp_count
++;
}
if
(
$act
==
$this
->page_num )
{
$tmp
[
$tmp_count
]['url'] = '';
$tmp
[
$tmp_count
]['name'] =
"尾页"
;
$tmp
[
$tmp_count
]['no'] =
$next
;
}
else
{
$tmp
[
$tmp_count
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$next
,
$url
);
$tmp
[
$tmp_count
]['name'] =
"尾页"
;
$tmp
[
$tmp_count
]['no'] =
$next
;
}
return
$tmp
;
}
function
make_page_with_points(
$total
,
$act_page
,
$url
,
$maxpageicon
,
$style
,
$param
)
{
$page_num
=
$this
->Calculation_Page_Num(
$total
);
$arr_min_max
=
$this
->Calculation_Min_Max(
$act_page
);
if
(
$total
==0)
{
return
""
;
}
if
(
$act_page
>
$page_num
)
{
$act_page
=
$page_num
+1;
$page_num
=
$page_num
+1;
}
switch
(
$style
){
case
1:
$name_before
= '前一页';
$name_next
= '后一页';
break
;
case
2:
$name_before
= '<';
$name_next
= '>';
break
;
case
3:
$name_before
= '<<';
$name_next
= '>>';
break
;
default
:
$name_before
= '上一页';
$name_next
= '下一页';
}
if
(!
eregi
(
"([?|&]$param=)"
,
$url
)) {
$url
=
strpos
(
$url
,
"?"
)===false?
$url
.
"?"
:
$url
.
"&"
;
$url
=
$url
.
"$param=0"
;
}
$url
=
eregi_replace
(
$param
. '=[0-9]+',
$param
. '=0',
$url
);
$res
=
array
();
$no_before
=
$act_page
-1;
$no_next
=
$act_page
+1;
if
(
$page_num
<= (
$maxpageicon
+ 1))
{
if
(
$act_page
== 1)
{
$res
[0]['url'] = '';
$res
[0]['name'] =
$name_before
;
$res
[0]['no'] =
$no_before
;
}
else
{
$res
[0]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .(
$act_page
- 1),
$url
);
$res
[0]['name'] =
$name_before
;
$res
[0]['no'] =
$no_before
;
}
$d
= 1;
for
(
$i
= 1;
$i
<=
$page_num
;
$i
++)
{
if
(
$i
!=
$act_page
)
{
$res
[
$d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$i
,
$url
);
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
else
{
$res
[
$d
]['url'] = '';
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
$res
[
$d
]['attr'] = 'current';
}
$d
++;
}
$last_d
=
count
(
$res
);
if
(
$act_page
==
$page_num
)
{
$res
[
$last_d
]['url'] = '';
$res
[
$last_d
]['name'] =
$name_next
;
$res
[
$last_d
]['no'] =
$no_next
;
}
else
{
$res
[
$last_d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .(
$act_page
+ 1),
$url
);
$res
[
$last_d
]['name'] =
$name_next
;
$res
[
$last_d
]['no'] =
$no_next
;
}
}
else
if
(
$page_num
> (
$maxpageicon
+ 1))
{
if
(
$act_page
<=
$maxpageicon
)
{
if
(
$act_page
== 1)
{
$res
[0]['url'] = '';
$res
[0]['name'] =
$name_before
;
$res
[0]['no'] =
$no_before
;
}
else
{
$res
[0]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .(
$act_page
- 1),
$url
);
$res
[0]['name'] =
$name_before
;
$res
[0]['no'] =
$no_before
;
}
$d
= 1;
for
(
$i
= 1;
$i
<=
$maxpageicon
;
$i
++)
{
if
(
$i
!=
$act_page
)
{
$res
[
$d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$i
,
$url
);
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
else
{
$res
[
$d
]['url'] = '';
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
$res
[
$d
]['attr'] = 'current';
}
$d
++;
}
$last_d
=
count
(
$res
);
$res
[
$last_d
]['url'] = '';
$res
[
$last_d
]['name'] = '...';
$res
[
$last_d
]['no'] = '';
$res
[
$last_d
+1]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$page_num
,
$url
);
$res
[
$last_d
+1]['name'] =
$page_num
;
$res
[
$last_d
+1]['no'] =
$page_num
;
$res
[
$last_d
+1]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' . (
$act_page
+ 1),
$url
);
$res
[
$last_d
+1]['name'] =
$name_next
;
$res
[
$last_d
+1]['no'] =
$no_next
;
}
else
{
if
(
$act_page
> (
$page_num
-
$maxpageicon
))
{
$res
[0]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .(
$act_page
- 1),
$url
);
$res
[0]['name'] =
$name_before
;
$res
[0]['no'] =
$no_before
;
$res
[1]['url'] =
str_replace
(
$param
. '=0',
$param
. '=1',
$url
);
$res
[1]['name'] = 1;
$res
[1]['no'] = 1;
$res
[2]['url'] = '';
$res
[2]['name'] = '...';
$res
[2]['no'] = '';
$d
= 3;
for
(
$i
= (
$page_num
-
$maxpageicon
+ 1);
$i
<=
$page_num
;
$i
++)
{
if
(
$i
!=
$act_page
)
{
$res
[
$d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$i
,
$url
);
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
else
{
$res
[
$d
]['url'] = '';
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
$res
[
$d
]['attr'] = 'current';
}
$d
++;
}
$last_d
=
count
(
$res
);
if
(
$act_page
==
$page_num
)
{
$res
[
$last_d
]['url'] = '';
$res
[
$last_d
]['name'] =
$name_next
;
$res
[
$last_d
]['no'] =
$no_next
;
}
else
{
$res
[
$last_d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .(
$act_page
+ 1),
$url
);
$res
[
$last_d
]['name'] =
$name_next
;
$res
[
$last_d
]['no'] =
$no_next
;
}
}
else
{
$res
[0]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .(
$act_page
- 1),
$url
);
$res
[0]['name'] =
$name_before
;
$res
[0]['no'] =
$no_before
;
$res
[1]['url'] =
str_replace
(
$param
. '=0',
$param
. '=1',
$url
);
$res
[1]['name'] = 1;
$res
[1]['no'] = 1;
$res
[2]['url'] = '';
$res
[2]['name'] = '...';
$res
[2]['no'] = '';
for
(
$i
= (
$act_page
- (
$maxpageicon
- 2) / 2);
$i
<=
floor
(
$act_page
+(
$maxpageicon
- 2) / 2);
$i
++)
{
$i
=
ceil
(
$i
);
if
(
$i
!=
$act_page
)
{
$res
[
$d
]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$i
,
$url
);
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
}
else
{
$res
[
$d
]['url'] = '';
$res
[
$d
]['name'] =
$i
;
$res
[
$d
]['no'] =
$i
;
$res
[
$d
]['attr'] = 'current';
}
$d
++;
}
$last_d
=
count
(
$res
);
$res
[
$last_d
]['url'] = '';
$res
[
$last_d
]['name'] = '...';
$res
[
$last_d
]['no'] = '';
$res
[
$last_d
+1]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' .
$page_num
,
$url
);
$res
[
$last_d
+1]['name'] =
$page_num
;
$res
[
$last_d
+1]['no'] =
$page_num
;
$res
[
$last_d
+2]['url'] =
str_replace
(
$param
. '=0',
$param
. '=' . (
$act_page
+ 1),
$url
);
$res
[
$last_d
+2]['name'] =
$name_next
;
$res
[
$last_d
+2]['no'] =
$no_next
;
}
}
}
return
$res
;
}
}
?>