封装分页类
来源:互联网 发布:剑倚天下进阶数据最新 编辑:程序博客网 时间:2024/05/17 01:01
<?php
class Page{
protected $total; //数据的总条数
protected $listRows; //一页显示多少条
protected $currentPage; //当前页
protected $countPage; //总的页数
public function __construct($total,$listRows="10"){
$this->total=$total;
$this->listRows=$listRows;
if(isset($_GET['page']) && $_GET['page']>0 ){
$this->currentPage=$_GET['page'];
}else{
$this->currentPage=1;
}
if($this->total<=0){
return '';
}else{
$this->countPage=ceil($this->total / $this->listRows);
}
}
function showPage(){
$url=$_SERVER['REQUEST_URI'];
$parse_url=parse_url($url);
//print_r($parse_url);
if(!isset($parse_url['query'])){
$pase_url['query']="page=".$this->currentPage;
}
//print_r($parse_url['query']);
parse_str($parse_url['query'],$parms);
if(!array_key_exists('page',$parms)){
$parms['page']=$this->currentPage;
}
if(count($parms)==1){
$url=$parse_url['path']."?";
// echo $url;
}else{
unset($parms['page']);
$url=$parse_url['path']."?".http_build_query($parms)."&";
//print_r($url);
}
$prev=$this->currentPage-1;
$next=$this->currentPage+1;
$header='<a href="' . $url .'page=' . 1 . '">首页</a>';
$last='<a href="' . $url .'page=' . $this->countPage . '">尾页</a>';
if($prev<1){
$prevPage='';
}else{
$prevPage='<a href="' . $url .'page=' . $prev . '">上一页</a>';
}
if($next>$this->countPage){
$nextPage='';
}else{
$nextPage='<a href="' . $url .'page=' . $next . '">下一页</a>';
}
$start = $this->currentPage - (5-1)/2; //计算左侧开始的页码
$end = $this->currentPage + (5-1)/2;//计算右侧开始的页码
//如果左侧的页面,已经小于1,则把小于1 的部分补到右侧
if ($start < 1) {
$end += (1 - $start);
$start = 1; //修改start = 1
if ($end > $this->countPage) {
$end = $this->countPage;
}
}
//把右侧超出的部分,补到左边
if ($end > $this->countPage) {
$start -= ($end - $this->countPage);
$end = $this->countPage;
if ($start < 1) {
$start = 1;
}
}
$pagestr='';
for ($i=$start; $i <= $end ; $i++) {
if ($i == $this->currentPage) {
$pagestr .= $i;
continue;
}
$pagestr.='<a href="' . $url . 'page=' . $i . '">' . $i . '</a>';
}
return $header.$prevPage.$pagestr.$nextPage.$last;
}
}
$page=new Page(30,3);
echo $page->showPage();
?>
class Page{
protected $total; //数据的总条数
protected $listRows; //一页显示多少条
protected $currentPage; //当前页
protected $countPage; //总的页数
public function __construct($total,$listRows="10"){
$this->total=$total;
$this->listRows=$listRows;
if(isset($_GET['page']) && $_GET['page']>0 ){
$this->currentPage=$_GET['page'];
}else{
$this->currentPage=1;
}
if($this->total<=0){
return '';
}else{
$this->countPage=ceil($this->total / $this->listRows);
}
}
function showPage(){
$url=$_SERVER['REQUEST_URI'];
$parse_url=parse_url($url);
//print_r($parse_url);
if(!isset($parse_url['query'])){
$pase_url['query']="page=".$this->currentPage;
}
//print_r($parse_url['query']);
parse_str($parse_url['query'],$parms);
if(!array_key_exists('page',$parms)){
$parms['page']=$this->currentPage;
}
if(count($parms)==1){
$url=$parse_url['path']."?";
// echo $url;
}else{
unset($parms['page']);
$url=$parse_url['path']."?".http_build_query($parms)."&";
//print_r($url);
}
$prev=$this->currentPage-1;
$next=$this->currentPage+1;
$header='<a href="' . $url .'page=' . 1 . '">首页</a>';
$last='<a href="' . $url .'page=' . $this->countPage . '">尾页</a>';
if($prev<1){
$prevPage='';
}else{
$prevPage='<a href="' . $url .'page=' . $prev . '">上一页</a>';
}
if($next>$this->countPage){
$nextPage='';
}else{
$nextPage='<a href="' . $url .'page=' . $next . '">下一页</a>';
}
$start = $this->currentPage - (5-1)/2; //计算左侧开始的页码
$end = $this->currentPage + (5-1)/2;//计算右侧开始的页码
//如果左侧的页面,已经小于1,则把小于1 的部分补到右侧
if ($start < 1) {
$end += (1 - $start);
$start = 1; //修改start = 1
if ($end > $this->countPage) {
$end = $this->countPage;
}
}
//把右侧超出的部分,补到左边
if ($end > $this->countPage) {
$start -= ($end - $this->countPage);
$end = $this->countPage;
if ($start < 1) {
$start = 1;
}
}
$pagestr='';
for ($i=$start; $i <= $end ; $i++) {
if ($i == $this->currentPage) {
$pagestr .= $i;
continue;
}
$pagestr.='<a href="' . $url . 'page=' . $i . '">' . $i . '</a>';
}
return $header.$prevPage.$pagestr.$nextPage.$last;
}
}
$page=new Page(30,3);
echo $page->showPage();
?>
0 1
- 数据分页封装类
- 分页封装成类
- 分页类的封装
- java 分页封装类
- 封装分页类PageInfo
- 封装分页类
- 封装分页类
- 分页封装类
- yii2.0 使用分页封装类 分页
- php封装分页类+分页实例
- 常用类封装--分页类
- 对于分页的封装类
- 分页的封装工具类
- 封装好的分页类
- 封装好的分页类
- 封装分页类之二
- 封装分页
- 分页封装
- Servlet生命周期
- gsoap 注意事项
- Hadoop深入研究(HDFS)---HDFS介绍
- Android应用内语言切换实现
- Android的RadioButton和checkBox的用法-android学习之旅(十九)
- 封装分页类
- poj1436 Horizontally Visible Segments
- uva 10106 Product(高精度大数乘法)
- maven基本使用
- Linux下使用Supervise
- Gh0st源码学习(三)生成DLL和DAT文件
- 使用javaswing设定背景图片为窗体大小(原创)
- php 5.4.38
- oracle wallet使用与维护