PHP封装的page分页类定义与用法完整示例

 更新时间:2019年2月26日 16:27  点击:310

本文实例讲述了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 = "&#63;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"  "
  <html xmlns="
     <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&#63;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>        <&#63;php        if (!empty($result)) {          foreach ($result as $k => $v) {            &#63;>            <tr align="center">              <td><&#63;php echo $v['id']; &#63;></td>              <td><&#63;php echo $v["customer_id"]; &#63;></td>              <td><&#63;php echo $v["name"]; &#63;></td>              <td><&#63;php echo $v["role_id"]; &#63;></td>              <td><&#63;php echo $v["status"]; &#63;></td>              <td><&#63;php echo $v["cdate"]; &#63;></td>            </tr>            <&#63;php          }        }        &#63;>      </table>    </div>    <div class="showPage">      <&#63;php      if ($total > $showrow) {//总记录数大于每页显示数,显示分页        $page = new page($total, $showrow, $curpage, $url, 3);        echo $page->myde_write();      }      &#63;>    </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;  }}&#63;>

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}


[!--infotagslink--]

相关文章

  • php KindEditor文章内分页的实例方法

    我们这里介绍php与KindEditor编辑器使用时如何利用KindEditor编辑器的分页功能实现文章内容分页,KindEditor编辑器在我们点击分页时会插入代码,我们只要以它为分切符,就...2016-11-25
  • 自己动手写的jquery分页控件(非常简单实用)

    最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • vue.js 表格分页ajax 异步加载数据

    Vue.js通过简洁的API提供高效的数据绑定和灵活的组件系统.这篇文章主要介绍了vue.js 表格分页ajax 异步加载数据的相关资料,需要的朋友可以参考下...2016-10-20
  • Springboot如何使用mybatis实现拦截SQL分页

    这篇文章主要介绍了Springboot使用mybatis实现拦截SQL分页,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-19
  • PHP 一个完整的分页类(附源码)

    在php中要实现分页比起asp中要简单很多了,我们核心就是直接获取当前页面然后判断每页多少再到数据库中利用limit就可以实现分页查询了,下面我来详细介绍分页类实现程序...2016-11-25
  • jquery实现的伪分页效果代码

    本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
  • AngularJS实现分页显示数据库信息

    这篇文章主要为大家详细介绍了AngularJS实现分页显示数据库信息效果的相关资料,感兴趣的小伙伴们可以参考一下...2016-07-06
  • 基于jquery实现表格无刷新分页

    这篇文章主要介绍了基于jquery实现表格无刷新分页,功能实现了前端排序功能,增加了前端搜索功能,感兴趣的小伙伴们可以参考一下...2016-01-08
  • vue实现页面打印自动分页的两种方法

    这篇文章主要为大家详细介绍了vue实现页面打印自动分页的两种方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-29
  • vue.js表格分页示例

    这篇文章主要为大家详细介绍了vue.js表格分页示例,ajax异步加载数据...2016-10-20
  • C# DataTable分页处理实例代码

    有时候我们从数据库获取的数据量太大,而我们不需要一次性显示那么多的时候,我们就要对数据进行分页处理了,让每页显示不同的数据。...2020-06-25
  • Python优化列表接口进行分页示例实现

    最近,在做测试开发平台的时候,需要对测试用例的列表进行后端分页,在实际去写代码和测试的过程中,发现这里面还是有些细节的,故想复盘一下...2021-09-29
  • 原生js实现分页效果

    这篇文章主要为大家详细介绍了原生js实现分页效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-09-24
  • laypage分页控件使用实例详解

    这篇文章主要为大家详细分享了laypage分页控件使用实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-05-20
  • 解析iReport自定义行数分页的操作方法

    ireport默认都是自动分页数据超出页面长度就会自动分到下一页,但有时候业务需要一页只显示固定几行这时候就需要自定义条数了。下面看具体操作吧...2021-10-26
  • MySQL分页优化

    这篇文章主要为大家详细介绍了MySQL分页优化,内容思路很详细,有意对MySQL分页优化的朋友可以参考一下...2016-04-22
  • EasyUI Pagination 分页的两种做法小结

    这篇文章主要介绍了EasyUI Pagination 分页的两种做法小结的相关资料,需要的朋友可以参考下...2016-07-25
  • 基于BootStrap的前端分页带省略号和上下页效果

    这篇文章主要介绍了基于BootStrap的前端分页带省略号和上下页效果,需要的朋友可以参考下...2017-05-22
  • 编写PHP脚本来实现WordPress中评论分页的功能

    这篇文章主要介绍了编写PHP脚本来实现WordPress中评论分页的功能的方法,包括上一页下一页和导航式分页功能的添加,需要的朋友可以参考下...2015-12-14