简易分页类

xuzuning 2012-08-28 06:57:08
加精
这是一个简单易用的分页类。只需在你原有的程序中加两句、改一句就可以了
先贴代码
paging.php
<?php
class Paging {
public static $count = 0;
public static $size = 0;
public static $page = 0;
static function prepare($sql, $pagesize=10) {
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageon = ($page - 1) * $pagesize;
$sql = preg_replace('/select\s/i', '$0SQL_CALC_FOUND_ROWS ', $sql) . " limit $pageon, $pagesize";
$rs = mysql_query($sql);
$p = mysql_query('SELECT FOUND_ROWS()');
list(self::$count) = mysql_fetch_row($p);
self::$size = $pagesize;
self::$page = $page;
return $rs;
}
static function bar($tpl='') {
if(!$tpl) $tpl = '<a href=?reset>首页</a> <a href=?prve>上一页</a> <a href=?next>下一页</a> <a href=?end>尾页</a>';
$count = ceil(self::$count / self::$size);
$page = self::$page;
unset($_GET['page']);
$d = array(
'reset' => 1,
'prve' => $page > 1 ? $page - 1 : 1,
'next' => $page < $count ? $page + 1 : $count,
'end' => $count,
);
foreach($d as $k=>$v) {
$_GET['page'] = $v;
$tpl = str_replace($k, http_build_query($_GET), $tpl);
}
echo $tpl;
}
}

通常你都有类似这样的语句
$sql =".....";
$rs = mysql_query($sql);

$rs = mysql_query("select ....");


你只需改作
include 'paging.php';
$rs = paging::prepare($sql, 每页行数);
在需要出现分页条的地方写入
paging::bar();

就可以了,非常简单!
...全文
7805 58 打赏 收藏 转发到动态 举报
写回复
用AI写文章
58 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰月渊 2013-08-31
  • 打赏
  • 举报
回复
哎、看来我的分页也要修改了!参考参考、
  • 打赏
  • 举报
回复
正在试着修改我的分页代码
杰克船长_ 2013-04-27
  • 打赏
  • 举报
回复
有木有和smarty 结合使用的分页啊
egou600 2012-09-13
  • 打赏
  • 举报
回复
可能是我单纯吧, 我进来拿分
卿柠 2012-09-13
  • 打赏
  • 举报
回复
到这里,绝非偶然,感激不尽!
tottyandbaty 2012-09-07
  • 打赏
  • 举报
回复
见识了,。先收藏备用
chenxiang_nihao 2012-09-06
  • 打赏
  • 举报
回复
代码太多了,看花了眼
huangzehuanhuan 2012-09-06
  • 打赏
  • 举报
回复
太强大的代码了 支持啊
wanghj_1118 2012-09-06
  • 打赏
  • 举报
回复
看不懂,理解不透,怎么办?php如何继续?都没信心了
mu_rain 2012-09-06
  • 打赏
  • 举报
回复
[Quote=引用 66 楼 的回复:]

代码太多了,看花了眼
[/Quote]
很无语!
a498976537 2012-09-05
  • 打赏
  • 举报
回复
很不错的帖
xuzuning 2012-09-04
  • 打赏
  • 举报
回复
[Quote=引用 56 楼 的回复:]

引用 42 楼 的回复:

<?php
include 'paging.php';
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("qq", $con);

//$……
[/Quote]你得吧我#6的代码(第二段)保存于paging.php中
yuwentao_528 2012-09-04
  • 打赏
  • 举报
回复
谢谢,本人主要是是来学习一下的!
_回到起点_ 2012-09-04
  • 打赏
  • 举报
回复
做个记号
lcyw 2012-09-03
  • 打赏
  • 举报
回复
[Quote=引用 42 楼 的回复:]

<?php
include 'paging.php';
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("qq", $con);

//$result = my……
[/Quote]



我用了你的例子, 但是显示错误,

Fatal error: Call to undefined function mysql_paging_query() in D:\PHPnow-1.5.6\htdocs\test\test.php on line 12


mysql_paging_query()?怎么找不到呢? 需要什么设置?
marcor 2012-09-02
  • 打赏
  • 举报
回复
收藏了~
hamw2009 2012-09-02
  • 打赏
  • 举报
回复
我觉得既然是分页就应该只负责构造分页的字符串即可。数据的提取等操作分开。
jichengbiange 2012-09-02
  • 打赏
  • 举报
回复
$sql = preg_replace('/select\s/i', '$0SQL_CALC_FOUND_ROWS ', $sql) . " limit $pageon, $pagesize";
这行代码里面的$0SQL_CALC_FOUND_ROWS 是不是写错了,应该是SQL_CALC_FOUND_ROWS吧?
xuzuning 2012-09-02
  • 打赏
  • 举报
回复
[Quote=引用 44 楼 的回复:]

引用 42 楼 的回复:
<?php
include 'paging.php';
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("qq", $con);

//$re……
[/Quote]有什么不会有的?
#42 是你 #41 代码,红色的部分是要改动的部分
xuzuning 2012-09-02
  • 打赏
  • 举报
回复
当然,有 SQL_CALC_FOUND_ROWS 是要比无 SQL_CALC_FOUND_ROWS 慢一些的
加载更多回复(38)

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧