php封装的page分页类完整实例代码


Posted in PHP onFebruary 01, 2020

效果图

php封装的page分页类完整实例代码

1.测试实例test.php

<?php
header("Content-Type: text/html; charset=utf-8");
date_default_timezone_set("Asia/Shanghai"); //时区

require_once('page.class.php');

$showrow = 5;
$curpage = empty($_GET['page']) ? 1 : $_GET['page'];
$url = "?page={page}";
$dsn = 'mysql:host=xxx.xxx.80.xxx;dbname=admin';
$pdo = new PDO($dsn, 'root', 'root');
$pdo->query('set names utf8');
$sql = "SELECT * from operator_list where 1=1";

$res_gg = $pdo->query("SELECT count(*) as ctn from operator_list where 1=1;");
$result = $res_gg->fetch();

$total = $result["ctn"];

if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow)) {
 $curpage = ceil($total_rows / $showrow);
}
$sql .= " LIMIT " . ($curpage - 1) * $showrow . ",$showrow;";

$res_zz = $pdo->query($sql);
$result = $res_zz->fetchAll();

//print_r(json_encode($result));die;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title></title>
 <meta name="keywords" content="入库"/>
 <meta name="description" content="入库"/>
 <script type="text/javascript" src="static/js/jquery-1.11.0.min.js?v=1"></script>
 <link rel="stylesheet" type="text/css" href="static/css/common.css" rel="external nofollow" />
</head>
<body>
<div class="head">
 <!-- <div class="head_inner clearfix">-->
 <!--  <ul id="nav">-->
 <!--   <li><a href="javascript;" rel="external nofollow" rel="external nofollow" >商品列表</a></li>-->
 <!--   <li><a href="javascript;" rel="external nofollow" rel="external nofollow" >详情列表</a></li>-->
 <!--  </ul>-->
 <!--    <a class="logo" href="javascript" rel="external nofollow" >
      <img src="javascript;" alt="公司logo" /></a> -->
 <!-- </div>-->
</div>
<div class="container">
 <div class="demo">
  <h2 class="title">报表</h2>

  <div class="showData">
   <table width="100%" border="0" align="center" 
   style="border:1px solid #ccc;" cellpadding="0" cellspacing="1">
    <tr align="center">
     <td>ID</td>
     <td>商品编号</td>
     <td>订阅状态</td>
     <td>商品状态</td>
     <td>修改时间</td>
     <td>创建时间</td>
    </tr>
    <?php
    if (!empty($result)) {
     foreach ($result as $k => $v) {
      ?>
      <tr align="center">
       <td><?php echo $v['id']; ?></td>
       <td><?php echo $v["customer_id"]; ?></td>
       <td><?php echo $v["name"]; ?></td>
       <td><?php echo $v["role_id"]; ?></td>
       <td><?php echo $v["status"]; ?></td>
       <td><?php echo $v["cdate"]; ?></td>
      </tr>
      <?php
     }
    }
    ?>
   </table>
  </div>
  <div class="showPage">
   <?php
   if ($total > $showrow) {//总记录数大于每页显示数,显示分页
    $page = new page($total, $showrow, $curpage, $url, 3);
    echo $page->myde_write();
   }
   ?>
  </div>
 </div>
</div>
<div class="foot">
 阿里巴巴:<a href="#" rel="external nofollow" target="_blank">https://www.taobao.com</a>
</div>
</body>
</html>

2.封装的page分页类page.class.php

<?php

/* * *********************************************
 * @类名: page
 * @参数: $myde_total - 总记录数
 *   $myde_size - 一页显示的记录数
 *   $myde_page - 当前页
 *   $myde_url - 获取当前的url
 * @功能: 分页实现
 */

class page {

 private $myde_total;   //总记录数
 private $myde_size;   //一页显示的记录数
 private $myde_page;   //当前页
 private $myde_page_count;  //总页数
 private $myde_i;    //起头页数
 private $myde_en;    //结尾页数
 private $myde_url;   //获取当前的url
 /*
  * $show_pages
  * 页面显示的格式,显示链接的页数为2*$show_pages+1。
  * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
  */
 private $show_pages;

 public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {
  $this->myde_total = $this->numeric($myde_total);
  $this->myde_size = $this->numeric($myde_size);
  $this->myde_page = $this->numeric($myde_page);
  $this->myde_page_count = ceil($this->myde_total / $this->myde_size);
  $this->myde_url = $myde_url;
  if ($this->myde_total < 0)
   $this->myde_total = 0;
  if ($this->myde_page < 1)
   $this->myde_page = 1;
  if ($this->myde_page_count < 1)
   $this->myde_page_count = 1;
  if ($this->myde_page > $this->myde_page_count)
   $this->myde_page = $this->myde_page_count;
  $this->limit = ($this->myde_page - 1) * $this->myde_size;
  $this->myde_i = $this->myde_page - $show_pages;
  $this->myde_en = $this->myde_page + $show_pages;
  if ($this->myde_i < 1) {
   $this->myde_en = $this->myde_en + (1 - $this->myde_i);
   $this->myde_i = 1;
  }
  if ($this->myde_en > $this->myde_page_count) {
   $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
   $this->myde_en = $this->myde_page_count;
  }
  if ($this->myde_i < 1)
   $this->myde_i = 1;
 }

 //检测是否为数字
 private function numeric($num) {
  if (strlen($num)) {
   if (!preg_match("/^[0-9]+$/", $num)) {
    $num = 1;
   } else {
    $num = substr($num, 0, 11);
   }
  } else {
   $num = 1;
  }
  return $num;
 }

 //地址替换
 private function page_replace($page) {
  return str_replace("{page}", $page, $this->myde_url);
 }

 //首页
 private function myde_home() {
  if ($this->myde_page != 1) {
   return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";
  } else {
   return "<p>首页</p>";
  }
 }

 //上一页
 private function myde_prev() {
  if ($this->myde_page != 1) {
   return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title='上一页'>上一页</a>";
  } else {
   return "<p>上一页</p>";
  }
 }

 //下一页
 private function myde_next() {
  if ($this->myde_page != $this->myde_page_count) {
   return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title='下一页'>下一页</a>";
  } else {
   return"<p>下一页</p>";
  }
 }

 //尾页
 private function myde_last() {
  if ($this->myde_page != $this->myde_page_count) {
   return "<a href='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</a>";
  } else {
   return "<p>尾页</p>";
  }
 }

 //输出
 public function myde_write($id = 'page') {
  $str = "<div id=" . $id . ">";
  $str.=$this->myde_home();
  $str.=$this->myde_prev();
  if ($this->myde_i > 1) {
   $str.="<p class='pageEllipsis'>...</p>";
  }
  for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
   if ($i == $this->myde_page) {
    $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";
   } else {
    $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";
   }
  }
  if ($this->myde_en < $this->myde_page_count) {
   $str.="<p class='pageEllipsis'>...</p>";
  }
  $str.=$this->myde_next();
  $str.=$this->myde_last();
  $str.="<p class='pageRemark'>共<b>" . $this->myde_page_count .
    "</b>页<b>" . $this->myde_total . "</b>条数据</p>";
  $str.="</div>";
  return $str;
 }

}

?>

3.css样式

html, body, div, span, h1, h2, h3, h4, h5, h6, p, pre, 
a, code, em, img, small, strong, sub, sup, u, i, center, 
dl, dt, dd, ol, ul, li, fieldset, form, label {
 margin: 0;
 padding: 0;
 border: 0;
 outline: 0;
 font-size: 100%;
 vertical-align: baseline;
 background: transparent
}

a {
 color: #007bc4;
 text-decoration: none;
 cursor: pointer;
}

.table_parameters a:hover {
 text-decoration: none
}

a:hover {
 text-decoration: underline
}

ol, ul {
 list-style: none
}

table {
 border-collapse: collapse;
 border-spacing: 0
}

/*html {*/
/*background: url(../images/demo_bg.png)*/
/*}*/

body {
 height: 100%;
 font: 12px/18px "Microsoft Yahei", Tahoma, Helvetica, 
 Arial, Verdana, "\5b8b\4f53", sans-serif;
 color: #51555c
}

img {
 border: 0;
 cursor: pointer;
}

.clearfix:after {
 visibility: hidden;
 display: block;
 font-size: 0;
 content: " ";
 clear: both;
 height: 0
}

.head {
 /*border-bottom: 1px solid #dadada;*/
 padding: 0 0 5px
}

.head_inner {
 margin: 0 auto;
 width: 980px
}

.container {
 width: 80%;
 /*min-height: 600px;*/
 margin: 30px auto 0 auto;
 border: 1px solid #d3d3d3;
 background: #fff;
 -moz-border-radius: 5px;
 -khtml-border-radius: 5px;
 -webkit-border-radius: 5px;
 border-radius: 5px
}

.demo > h2.title {
 margin: 4px 0 30px;
 padding: 15px 0 10px 20px;
 border-bottom: 1px solid #d3d3d3;
 font-size: 18px;
 color: #a84c10;
 background: url(../images/arrow.jpg) no-repeat 2px 14px
}

.foot {
 height: 60px;
 padding: 10px 2px;
 line-height: 24px;
 text-align: center
}

.foot a:hover {
 color: #51555c
}

.btn {
 -webkit-border-radius: 3px;
 -moz-border-radius: 3px;
 -ms-border-radius: 3px;
 -o-border-radius: 3px;
 border-radius: 3px;
 background-color: #ff8400;
 color: #fff;
 display: inline-block;
 height: 28px;
 line-height: 28px;
 text-align: center;
 padding: 0 12px;
 transition: background-color .2s linear 0s;
 border: 0;
 cursor: pointer
}

.btn:hover {
 background-color: #e95a00;
 text-decoration: none
}

.demo {
 width: 700px;
 margin: 0 auto
}

ul.ul_demo li {
 background: url("../images/demo_icon.gif") no-repeat scroll 0 6px;
 line-height: 28px;
 padding-left: 20px
}

.input, .table input[type='text'] {
 border: 1px solid #ccc;
 padding: 0 5px;
 width: 220px;
 height: 26px;
 line-height: 26px
}

#nav {
 float: right;
 margin: 30px 0 0
}

#nav li {
 float: left;
 font-size: 16px;
 margin-right: 20px
}

.btn.loading {
 opacity: .5
}

h3 a.cur {
 color: #f30;
}

.demo h3 a {
 font-size: 14px;
 margin: 0 10px 5px 0;
 display: inline-block
}

.red {
 color: red
}

.notice {
 font-size: 14px;
 margin-bottom: 10px;
}

.table_parameters {
 border-left: 1px solid #d3d3d3;
 border-top: 1px solid #d3d3d3;
 margin: 6px auto;
 font-size: 14px
}

.table_parameters tr.tr_head {
 background: none repeat scroll 0 0 #f7f7f7;
 font-weight: bold;
 padding: 6px;
 text-align: center
}

.table_parameters td, .table_parameters th {
 border-bottom: 1px solid #d3d3d3;
 border-right: 1px solid #d3d3d3;
 line-height: 26px;
 padding: 2px
}

h1 {
 font: 32px "Microsoft Yahei";
 margin: 40px auto;
 text-align: center;
}

h2 {
 font-size: 16px;
 margin: 10px 0;
}

.menu {
 height: 30px;
 margin-bottom: 30px;
 padding: 10px;
 background-color: #f0f0f0;
 text-align: center;
}

.menu a {
 display: inline-block;
 height: 30px;
 padding: 0 20px;
 line-height: 30px;
 font-size: 14px;
 color: #333;
 text-decoration: none;
}

.menu a:hover {
 color: #000;
 background-color: #e9e9e9;
}

.menu .cur {
 background-color: #ddd !important;
 color: #000;
}

.vad a {
 display: inline-block;
 height: 36px;
 line-height: 36px;
 margin: 0 5px;
 padding: 0 50px;
 font-size: 14px;
 text-align: center;
 color: #eee;
 text-decoration: none;
 background-color: #222;
}

.vad a:hover {
 color: #fff;
 background-color: #000;
}

.thead {
 width: 728px;
 height: 90px;
 margin: 0 auto;
}

textarea {
 border: 1px solid #ccc;
 font-size: 12px;
 height: 100px;
 line-height: 18px;
 padding: 5px;
 width: 300px;
}

.table td {
 padding: 10px 0
}

.disabled {
 opacity: .6;
 filter: alpha(opacity=60)
}
.demo > p {
 line-height: 30px;
 font-size: 14px
}

.demo > p a {
 font-size: 14px
}

.demo h3 {
 font-size: 16px;
 margin: 20px 0
}

select {
 background-color: #fff;
 background-position: right center;
 background-repeat: no-repeat;
 border: 1px solid #888;
 border-radius: 3px;
 box-sizing: border-box;
 font: 12px/1.5 Tahoma, Arial, sans-serif;
 height: 30px;
 padding: 0 4px;
}

fieldset {
 border: 1px solid #ccc;
 border-radius: 5px;
 margin: 1em 0;
 padding: 10px 20px;
}

dl.row dt {
 width: 2em;
}

dl.row dt {
 clear: left;
 float: left;
 line-height: 30px;
 padding: 5px;
 text-align: right;
 width: 6em;
}

dl.row dd {
 float: left;
 padding: 5px;
}

.pager {
 text-align: right;
}

.pager a {
 padding: 3px 8px;
 margin-left: 3px;
 line-height: 20px;
 background: #f9f9f9;
 border: 1px solid #DBDBDB;
 text-decoration: none
}

.pager a:hover,
.pager a.current {
 background-color: #7CD5B1;
 color: #fff;
 border: 1px solid #7CD5B1;
 cursor: pointer;
}

.page {
 text-align: center;
 margin: 50px 0
}

.page a, .page span.prev_disabled {
 border: 1px solid #ededed;
 color: #3d3d3d;
 font-weight: 700;
 height: 35px;
 line-height: 35px;
 margin-left: 5px;
 min-width: 9px;
 padding: 0 13px;
 text-align: center;
 text-decoration: none;
 vertical-align: top;
 font-family: "simsun";
 display: inline-block
}

.page span.prev_disabled {
 cursor: default;
 color: #ccc;
 margin: 0 10px 0 0
}

.page a.current {
 background-color: #f40;
 border-color: #f40;
 color: #fff;
 font-weight: 700;
 position: relative;
 z-index: 1;
}

.page .extra {
 display: inline-block;
 margin-left: 10px;
 height: 35px;
 line-height: 35px;
 color: #656565;
}

.page .page-num {
 border: 1px solid #ededed;
 height: 21px;
 text-align: center;
 width: 35px;
 display: inline-block
}

.page .page-submit {
 background-clip: padding-box;
 border: 1px solid #ededed;
 border-radius: 2px;
 cursor: pointer;
 height: 21px;
 line-height: 21px;
 text-align: center;
 width: 43px;
 display: inline-block
}

.page .page-submit:hover {
 border-color: #f40;
 color: #f40;
}

.page a:focus, .page a:hover {
 border-color: #f40;
 z-index: 1;
}

.loading {
 margin: 30px 0;
 text-align: center
}

p {
 margin: 0
}

#page {
 height: 40px;
 padding: 20px 0px;
}

#page a {
 display: block;
 float: left;
 margin-right: 10px;
 padding: 2px 12px;
 height: 24px;
 border: 1px #cccccc solid;
 background: #fff;
 text-decoration: none;
 color: #808080;
 font-size: 12px;
 line-height: 24px;
}

#page a:hover {
 color: #077ee3;
 border: 1px #077ee3 solid;
}

#page a.cur {
 border: none;
 background: #077ee3;
 color: #fff;
}

#page p {
 float: left;
 padding: 2px 12px;
 font-size: 12px;
 height: 24px;
 line-height: 24px;
 color: #bbb;
 border: 1px #ccc solid;
 background: #fcfcfc;
 margin-right: 8px;

}

#page p.pageRemark {
 border-style: none;
 background: none;
 margin-right: 0px;
 padding: 4px 0px;
 color: #666;
}

#page p.pageRemark b {
 color: red;
}

#page p.pageEllipsis {
 border-style: none;
 background: none;
 padding: 4px 0px;
 color: #808080;
}

.dates li {
 font-size: 14px;
 margin: 20px 0
}

.dates li span {
 text-align: center
}

td {
 font-size: 15px;
 margin: 20px 0
}

具体的大家可以多学习别人的php分页类然后多练就可以了。

PHP 相关文章推荐
Dedecms V3.1 生成HTML速度的优化办法
Mar 18 PHP
PHP注释实例技巧
Oct 03 PHP
php中使用cookie来保存用户登录信息的实现代码
Mar 08 PHP
深入PHP curl参数的详解
Jun 17 PHP
通过curl模拟post和get方式提交的表单类
Apr 23 PHP
phpcms手机内容页面添加上一篇和下一篇
Jun 05 PHP
关于PHP开发的9条建议
Jul 27 PHP
php的api数据接口书写实例(推荐)
Sep 22 PHP
PHP-FPM运行状态的实时查看及监控详解
Nov 18 PHP
PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID
Nov 25 PHP
php+Ajax无刷新验证用户名操作实例详解
Mar 04 PHP
Laravel 验证码认证学习记录小结
Dec 20 PHP
PHP实现简单的协程任务调度demo示例
Feb 01 #PHP
PHP设计模式之组合模式定义与应用示例
Feb 01 #PHP
php实现的简单多进程服务器类完整示例
Feb 01 #PHP
laravel 框架执行流程与原理简单分析
Feb 01 #PHP
laravel框架学习笔记之组件化开发实现方法
Feb 01 #PHP
php正则表达式使用方法整理集合
Jan 31 #PHP
laravel邮件发送的实现代码示例
Jan 31 #PHP
You might like
几种有用的变型 PHP中循环语句的用法介绍
2012/01/30 PHP
Laravel 模型关联基础教程详解
2019/09/17 PHP
理解JavaScript的caller,callee,call,apply
2009/04/28 Javascript
用Jquery.load载入页面实现局部刷新
2014/01/22 Javascript
jquery实现点击label的同时触发文本框点击事件的方法
2015/06/05 Javascript
原生JS实现仿淘宝网左侧商品分类菜单效果代码
2015/09/10 Javascript
JS实现仿微博可关闭弹出层效果
2015/09/21 Javascript
JS验证字符串功能
2017/02/22 Javascript
详解AngularJS 路由 resolve用法
2017/04/24 Javascript
angular2 ng2-file-upload上传示例代码
2018/08/23 Javascript
element-ui循环显示radio控件信息的方法
2018/08/24 Javascript
vue实现数字动态翻牌的效果(开箱即用)
2019/12/08 Javascript
JS实现简单移动端鼠标拖拽
2020/07/23 Javascript
jQuery实现放大镜案例
2020/10/19 jQuery
跟老齐学Python之数据类型总结
2014/09/24 Python
分析在Python中何种情况下需要使用断言
2015/04/01 Python
利用python爬取斗鱼app中照片方法实例
2017/12/03 Python
pandas DataFrame行或列的删除方法的实现示例
2019/08/02 Python
Python求平面内点到直线距离的实现
2020/01/19 Python
虚拟机下载python是否需要联网
2020/07/27 Python
canvas简单连线动画的实现代码
2020/02/04 HTML / CSS
HTML5输入框下拉菜单功能的示例代码
2020/09/08 HTML / CSS
KIKO MILANO荷兰网上商店:意大利专业化妆品品牌
2017/05/12 全球购物
享受加州生活方式的时尚舒适:XCVI
2018/07/09 全球购物
Timberland澳大利亚官网:全球领先的户外品牌
2019/12/10 全球购物
罗技英国官方网站:Logitech UK
2020/11/03 全球购物
网络安全方面的面试题
2015/11/04 面试题
Delphi软件工程师试题
2013/01/29 面试题
年会搞笑主持词
2014/03/27 职场文书
社区科普工作方案
2014/06/03 职场文书
代理词怎么写
2015/05/25 职场文书
大国崛起观后感
2015/06/02 职场文书
庆祝教师节新闻稿
2015/07/17 职场文书
详解Mysql 函数调用优化
2021/04/07 MySQL
MySQL 中如何归档数据的实现方法
2022/03/16 SQL Server
javascript进阶篇深拷贝实现的四种方式
2022/07/07 Javascript