php 3行代码的分页算法(求起始页和结束页)

yipeiwu_com6年前PHP代码库
一个好的分页算法, 应该具有下面的优点:

当前页码应该尽量在正中间.
如果"首页"和"尾页"不可用(当前处于第一页或最后一页), 不要隐藏这两组文字, 以免链接按钮位置变动.
算法简单.
下面的算法具有前面1和3两个优点.
复制代码 代码如下:

// $curr_index, 当前页码.
// $link_count, 链接数量.
// $page_count, 当前的数据的总页数.
// $start, 显示时的起始页码.
// $end, 显示时的终止页码.
$start = max(1, $curr_index - intval($link_count/2));
$end = min($start + $link_count - 1, $page_count);
$start = max(1, $end - $link_count + 1);
start = Math.max(1, curr_index - parseInt(link_count/2));
end = Math.min(page_count, start + link_count - 1);
start = Math.max(1, end - link_count + 1);

相关文章

php中mt_rand()随机数函数用法

本文实例讲述了php中mt_rand()随机数函数用法。分享给大家供大家参考。具体分析如下: mt_rand() 使用 mersenne twister 算法返回随机整数. 语法:mt_...

php empty函数 使用说明

Determine whether a variable is considered to be empty. 但是在我的记忆中,有很长一段时间一直以为empty应该是能够判断一个东西是...

php基于jquery的ajax技术传递json数据简单实例

本文实例讲述了php基于jquery的ajax技术传递json数据简单实现方法。分享给大家供大家参考,具体如下: html页面: <html> <head>...

如何获知PHP程序占用多少内存(memory_get_usage)

下面是使用示例: 复制代码 代码如下: <?php echo memory_get_usage(), '<br />'; // 313864 $tmp = str_re...

PHP实现的消息实时推送功能【基于反ajax推送】

本文实例讲述了PHP实现的消息实时推送功能。分享给大家供大家参考,具体如下: 入口文件index.html <!DOCTYPE HTML> <html> &l...