终于完成php的通用分页类了!呵,,有点高兴!以前说过要写一个这样的类的,无奈工作实在太忙,所以拖了下来。幸好今天是五一长假的最后一天,提前赶了回来,趁着老板不在,拼凑了出来。
还没有进行全面的测试,可能还存在各种的Bug,如果谁发现了,请告知一下,以便能及时的收正,谢谢!
源码如下:
<!--
类名:php通用分页类
功能:实现php中常用的页面分页,提供图像文字分页链接、图像分页链接。文字链接时,可自定义链接文字,图像链接时,可自定义图片。并且两种方式都可自定义样式。
作者:KnowMore
邮箱:hkmgjsf@yahoo
.
com
.
cn
电话:
15976526525
时间:
2007.5
.
3
附注:如发现Bug,麻烦告知一声,以便能及时纠正,谢谢!可自由转载,但请注明作者、出处。
-->
<?
class
page
{
var
$all_count
,
$cur_page
,
$sql
,
$conn
,
$page_size
,
$all_page
,
$cur_url
;
function
page(
$conn
)
//
构造函数
{
$this
->
conn
=
$conn
;
//
数据库连接
}
function
init()
//
初始化函数
{
$this
->
get_count();
//
获取总纪录数
$this
->
get_curpage();
//
获取当前页数
$this
->
get_allpage();
//
总页数
$this
->
cur_url
=
$_SERVER
[
'
PHP_SELF
'
];
//
当前页面url
}
function
open()
//
获取记录集
{
$this
->
init();
//
初始化
if
(
$this
->
conn)
{
$sql
=
$this
->
sql
.
"
limit
"
.
$this
->
page_size
*
$this
->
cur_page
.
"
,
"
.
$this
->
page_size;
$rs
=
mysql_query
(
$sql
,
$this
->
conn);
return
$rs
;
}
else
return
false
;
}
function
get_count()
//
获取总纪录数
{
if
(
isset
(
$_GET
[
'
all_count
'
]))
$this
->
all_count
=
$_GET
[
'
all_count
'
];
else
{
$rs
=
mysql_query
(
$this
->
sql
,
$this
->
conn);
$this
->
all_count
=
mysql_num_rows
(
$rs
);
}
}
function
get_curpage()
//
获取当前页数
{
if
(
isset
(
$_GET
[
'
cur_page
'
]))
$this
->
cur_page
=
intval
(
$_GET
[
'
cur_page
'
])
+
0
;
else
$this
->
cur_page
=
0
;
}
function
get_allpage()
//
获取总页数
{
if
(
$this
->
all_count
%
$this
->
page_size)
$this
->
all_page
=
intval
(
$this
->
all_count
/
$this
->
page_size);
else
$this
->
all_page
=
intval
(
$this
->
all_count
/
$this
->
page_size)
-
1
;
}
//
文字分页链接
function
first_page(
$name
=
"
首页
"
,
$str
=
""
,
$class_name
=
""
)
//
首页。$name为首页名称,$str为自定义传递字符串,$class_name为CSS类选择标签名(注,以下同)
{
if
(
$this
->
cur_page
>
0
)
//
当前页大于第一页
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'>$name</a>
"
,
$this
->
cur_url
,
0
,
$this
->
all_count);
}
function
back_page(
$name
=
"
上一页
"
,
$str
=
""
,
$class_name
=
""
)
//
上一页
{
if
(
$this
->
cur_page
>
0
)
//
当前页不是第一页
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'>$name</a>
"
,
$this
->
cur_url
,
$this
->
cur_page
-
1
,
$this
->
all_count);
}
function
next_page(
$name
=
"
下一页
"
,
$str
=
""
,
$class_name
=
""
)
//
下一页
{
if
(
$this
->
cur_page
<
$this
->
all_page)
//
当前页小于总页数
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'>$name</a>
"
,
$this
->
cur_url
,
$this
->
cur_page
+
1
,
$this
->
all_count);
}
function
last_page(
$name
=
"
尾页
"
,
$str
=
""
,
$class_name
=
""
)
//
尾页
{
if
(
$this
->
cur_page
<
$this
->
all_page)
//
当前页不是最后一页
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'>$name</a>
"
,
$this
->
cur_url
,
$this
->
all_page
,
$this
->
all_count);
}
//
文字分页链接
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
//图像分页链接
function
first_page_img(
$url
=
""
,
$str
=
""
,
$class_name
=
""
)
//
首页。$url为图片地址,$str为自定义传递字符串,$class_name为CSS类选择标签名(注,以下同)
{
if
(
$this
->
cur_page
>
0
)
//
当前页大于第一页
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'><img src='$url' border='0'/></a>
"
,
$this
->
cur_url
,
0
,
$this
->
all_count);
}
function
back_page_img(
$url
=
""
,
$str
=
""
,
$class_name
=
""
)
//
上一页
{
if
(
$this
->
cur_page
>
0
)
//
当前页不是第一页
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'><img src='$url' border='0' /></a>
"
,
$this
->
cur_url
,
$this
->
cur_page
-
1
,
$this
->
all_count);
}
function
next_page_img(
$url
=
""
,
$str
=
""
,
$class_name
=
""
)
//
下一页
{
if
(
$this
->
cur_page
<
$this
->
all_page)
//
当前页小于总页数
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'><img src='$url' border='0' /></a>
"
,
$this
->
cur_url
,
$this
->
cur_page
+
1
,
$this
->
all_count);
}
function
last_page_img(
$url
=
""
,
$str
=
""
,
$class_name
=
""
)
//
尾页
{
if
(
$this
->
cur_page
<
$this
->
all_page)
//
当前页不是最后一页
printf
(
"
<a href='%s?cur_page=%d&all_count=%d&$str' class='$class_name'><img src='$url' border='0' /></a>
"
,
$this
->
cur_url
,
$this
->
all_page
,
$this
->
all_count);
}
//
图像分页链接
}
?>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
测试代码就不多贴出来了,有兴趣的可参照如下文件:http://knowmore.blogbus.com/files/11781973580.php (另存为php文件就行了)