php 分页类
<?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改成正规的
$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改成正规的
$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;
} /**
* 带上一页<,下一页>,省略号的分页
* @param int $total 记录总条数
* @param int $act_page 当前页码
* @param string $url url
* @param int $maxpageicon 最大显示页码数
* @param int $style 上一页,下一页显示样式
* @param string $param url参数
*/
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改成正规的
$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;
//exit;
}
}
}
return $res;
}
} ?>
使用方法 :
$page_no = $_GET['page_no'] ? $_GET['page_no'] : 1;
$count = 20;
$start = ($page_no-1)*$count;
$end = $start + $count;
$page = new Page_Link();
$page->length = $count;
$page->isFirstPage = true;
$url = $_SERVER['SCRIPT_NAME']."?&a=".$a;
$page_arr = $page->make_page($total_num,$page_no,$url,"page_no");
支持多种样式:
php 分页类的更多相关文章
- php实现的分页类
php分页类文件: <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 pr ...
- asp.net的快捷实用分页类
KeleyiPager分页类,可以于对列表页进行分页浏览,代码是从HoverTreeCMS项目中COPY的,感觉很不错,使用简单方便,但是功能强大. 在线体验效果:http://cms.hovertr ...
- php分页类
1.需求 学会php分页类的使用 2.参考例子 CI的分页类 3.代码部分 <?php class pagination{ public $pagesize=20; public $pagein ...
- PHPCMS V9 分页类的修改教程
首先,打开 phpcms\libs\functions\global.func.php 这个文件,找到文件第622行的分页函数,复制一下,粘贴到默认分页函数的下面,重新命名后保存.(笔者在此命名为:p ...
- php 简单分页类
/** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...
- PHP简单漂亮的分页类
本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap. <?php /* * ********************************************* * @类名 ...
- ThinkPHP 分页类的使用及退出功能的实现
/* ThinkPHP设置编码统一: 一.数据库设置为utf8_bin 二.HTML页面设置charset=utf-8,而且检查文档编码格式是否是utf-8.phpDesigner8设置方式为“文件- ...
- php部分---一个分页类、用法
1.分页类 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 privat ...
- DedeCMS织梦动态分页类,datalist标签使用实例
<?php require_once(dirname(__FILE__)."/include/common.inc.php");//载入基础文件 require_once(D ...
- webpy分页类 + 上传类
webpy没有分页类.按照php的思路.自己编了一个.数据库用的是sqlite. class Page(object): '''分页类''' def __init__(self,page_size,d ...
随机推荐
- SSDB安装配置
1.下载ssdb wget -O ssdb-master.zip https://codeload.github.com/ideawu/ssdb/zip/master 2.编译安装 unzip ssd ...
- atitit. access token是什么??微信平台公众号开发access_token and Web session保持状态机制
atitit. access token是什么??微信平台公众号开发access_token and Web session保持状态机制 1. token机制and session保持状态机制 1 ...
- Atitit.可视化编程jbpm6 的环境and 使用总结...
Atitit.可视化编程jbpm6 的环境and 使用总结... 1. Jbpm的意义 1 2. Jbpm6环境配置 2 2.1. Down 2 2.2. Install eclipse jbpm p ...
- emoji表情引发的JNI崩溃
今天突然接到客服那边的反馈说,有玩家反馈进游戏后不久就崩溃了,我先是怀疑网络问题,因为一连接聊天成功后就挂了.之后用logcat抓日志,发现挂在jni那里了 JNI DETECTED ERROR IN ...
- 【转】IT 圈里有哪些经常被读错的词?
以下内容转至知乎,原文:http://www.zhihu.com/question/19739907?__nids__=5363833,5358751,5355121,5365018,5363846, ...
- 第六章 - 图像变换 - 图像拉伸、收缩、扭曲、旋转[1] - 仿射变换(cvWarpAffine)
拉伸.收缩.扭曲.旋转是图像的几何变换,在三维视觉技术中大量应用到这些变换,又分为仿射变换和透视变换.仿射变换通常用单应性建模,利用cvWarpAffine解决密集映射,用cvTransform解决稀 ...
- Scala 深入浅出实战经典 第46讲: ClassTag 、Manifest、ClasMainifest TagType实战
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- 表单验证Jquery扩展方法类
/** 表单数据验证 **/ $.fn.Validform = function () { var Validatemsg = ""; var Validateflag = tru ...
- Windows下安装 msysGit 以及初始化 Git server环境
Windows下git工具msysGit使用以及Git server初始化 Windows下git工具,这里选择msysGit,版本为msysGit-netinstall-1.8.1.2-previe ...
- puma vs passenger vs rainbows! vs unicorn vs thin 适用场景 及 performance
ruby的几个web server,按照开发活跃度.并发方案及要点.适用场景等分析puma vs passenger vs rainbows! vs unicorn vs thin. 1. thin: ...