温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在PHP中定义一个page分页类

发布时间:2021-04-15 17:05:24 阅读:113 作者:Leah 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家介绍怎么在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">
    <h3 class="title">报表</h3>
    <div class="showData">
      <table width="100%" border="0" align="center"
       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$url3);
        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, 011);
      }
    } 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样式

htmlbodydivspanh2h3h4h5h6, h7, p, pre,
acodeemimg, small, strong, sub, sup, u, i, center,
dldtddolullifieldsetformlabel {
  margin0;
  padding0;
  border0;
  outline0;
  font-size100%;
  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
}
olul {
  list-style: none
}
table {
  border-collapse: collapse;
  border-spacing0
}
/*html {*/
/*background: url(../images/demo_bg.png)*/
/*}*/
body {
  height100%;
  font12px/18px "Microsoft Yahei", Tahoma, Helvetica,
  Arial, Verdana, "\5b8b\4f53", sans-serif;
  color#51555c
}
img {
  border0;
  cursor: pointer;
}
.clearfix:after {
  visibility: hidden;
  display: block;
  font-size0;
  content" ";
  clear: both;
  height0
}
.head {
  /*border-bottom: 1px solid #dadada;*/
  padding0 0 5px
}
.head_inner {
  margin0 auto;
  width980px
}
.container {
  width80%;
  /*min-height: 600px;*/
  margin30px auto 0 auto;
  border1px solid #d3d3d3;
  background#fff;
  -moz-border-radius5px;
  -khtml-border-radius5px;
  -webkit-border-radius5px;
  border-radius5px
}
.demo > h3.title {
  margin4px 0 30px;
  padding15px 0 10px 20px;
  border-bottom1px solid #d3d3d3;
  font-size18px;
  color#a84c10;
  backgroundurl(../images/arrow.jpg) no-repeat 2px 14px
}
.foot {
  height60px;
  padding10px 2px;
  line-height24px;
  text-align: center
}
.foot a:hover {
  color#51555c
}
.btn {
  -webkit-border-radius3px;
  -moz-border-radius3px;
  -ms-border-radius3px;
  -o-border-radius3px;
  border-radius3px;
  background-color#ff8400;
  color#fff;
  display: inline-block;
  height28px;
  line-height28px;
  text-align: center;
  padding0 12px;
  transition: background-color .2s linear 0s;
  border0;
  cursor: pointer
}
.btn:hover {
  background-color#e95a00;
  text-decoration: none
}
.demo {
  width700px;
  margin0 auto
}
ul.ul_demo li {
  backgroundurl("../images/demo_icon.gif") no-repeat scroll 0 6px;
  line-height28px;
  padding-left20px
}
.input.table input[type='text'] {
  border1px solid #ccc;
  padding0 5px;
  width220px;
  height26px;
  line-height26px
}
#nav {
  float: right;
  margin30px 0 0
}
#nav li {
  float: left;
  font-size16px;
  margin-right20px
}
.btn.loading {
  opacity: .5
}
h4 a.cur {
  color#f30;
}
.demo h4 a {
  font-size14px;
  margin0 10px 5px 0;
  display: inline-block
}
.red {
  color: red
}
.notice {
  font-size14px;
  margin-bottom10px;
}
.table_parameters {
  border-left1px solid #d3d3d3;
  border-top1px solid #d3d3d3;
  margin6px auto;
  font-size14px
}
.table_parameters tr.tr_head {
  background: none repeat scroll 0 0 #f7f7f7;
  font-weight: bold;
  padding6px;
  text-align: center
}
.table_parameters td.table_parameters th {
  border-bottom1px solid #d3d3d3;
  border-right1px solid #d3d3d3;
  line-height26px;
  padding2px
}
h2 {
  font32px "Microsoft Yahei";
  margin40px auto;
  text-align: center;
}
h3 {
  font-size16px;
  margin10px 0;
}
.menu {
  height30px;
  margin-bottom30px;
  padding10px;
  background-color#f0f0f0;
  text-align: center;
}
.menu a {
  display: inline-block;
  height30px;
  padding0 20px;
  line-height30px;
  font-size14px;
  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;
  height36px;
  line-height36px;
  margin0 5px;
  padding0 50px;
  font-size14px;
  text-align: center;
  color#eee;
  text-decoration: none;
  background-color#222;
}
.vad a:hover {
  color#fff;
  background-color#000;
}
.thead {
  width728px;
  height90px;
  margin0 auto;
}
textarea {
  border1px solid #ccc;
  font-size12px;
  height100px;
  line-height18px;
  padding5px;
  width300px;
}
.table td {
  padding10px 0
}
.disabled {
  opacity: .6;
  filteralpha(opacity=60)
}
.demo > p {
  line-height30px;
  font-size14px
}
.demo > p a {
  font-size14px
}
.demo h4 {
  font-size16px;
  margin20px 0
}
select {
  background-color#fff;
  background-position: right center;
  background-repeat: no-repeat;
  border1px solid #888;
  border-radius3px;
  box-sizing: border-box;
  font12px/1.5 Tahoma, Arial, sans-serif;
  height30px;
  padding0 4px;
}
fieldset {
  border1px solid #ccc;
  border-radius5px;
  margin1em 0;
  padding10px 20px;
}
dl.row dt {
  width2em;
}
dl.row dt {
  clear: left;
  float: left;
  line-height30px;
  padding5px;
  text-align: right;
  width6em;
}
dl.row dd {
  float: left;
  padding5px;
}
.pager {
  text-align: right;
}
.pager a {
  padding3px 8px;
  margin-left3px;
  line-height20px;
  background#f9f9f9;
  border1px solid #DBDBDB;
  text-decoration: none
}
.pager a:hover,
.pager a.current {
  background-color#7CD5B1;
  color#fff;
  border1px solid #7CD5B1;
  cursor: pointer;
}
.page {
  text-align: center;
  margin50px 0
}
.page a.page span.prev_disabled {
  border1px solid #ededed;
  color#3d3d3d;
  font-weight700;
  height35px;
  line-height35px;
  margin-left5px;
  min-width9px;
  padding0 13px;
  text-align: center;
  text-decoration: none;
  vertical-align: top;
  font-family"simsun";
  display: inline-block
}
.page span.prev_disabled {
  cursor: default;
  color#ccc;
  margin0 10px 0 0
}
.page a.current {
  background-color#f40;
  border-color#f40;
  color#fff;
  font-weight700;
  position: relative;
  z-index1;
}
.page .extra {
  display: inline-block;
  margin-left10px;
  height35px;
  line-height35px;
  color#656565;
}
.page .page-num {
  border1px solid #ededed;
  height21px;
  text-align: center;
  width35px;
  display: inline-block
}
.page .page-submit {
  background-clip: padding-box;
  border1px solid #ededed;
  border-radius2px;
  cursor: pointer;
  height21px;
  line-height21px;
  text-align: center;
  width43px;
  display: inline-block
}
.page .page-submit:hover {
  border-color#f40;
  color#f40;
}
.page a:focus.page a:hover {
  border-color#f40;
  z-index1;
}
.loading {
  margin30px 0;
  text-align: center
}
p {
  margin0
}
#page {
  height40px;
  padding20px 0px;
}
#page a {
  display: block;
  float: left;
  margin-right10px;
  padding2px 12px;
  height24px;
  border1px #cccccc solid;
  background#fff;
  text-decoration: none;
  color#808080;
  font-size12px;
  line-height24px;
}
#page a:hover {
  color#077ee3;
  border1px #077ee3 solid;
}
#page a.cur {
  border: none;
  background#077ee3;
  color#fff;
}
#page p {
  float: left;
  padding2px 12px;
  font-size12px;
  height24px;
  line-height24px;
  color#bbb;
  border1px #ccc solid;
  background#fcfcfc;
  margin-right8px;
}
#page p.pageRemark {
  border-style: none;
  background: none;
  margin-right0px;
  padding4px 0px;
  color#666;
}
#page p.pageRemark b {
  color: red;
}
#page p.pageEllipsis {
  border-style: none;
  background: none;
  padding4px 0px;
  color#808080;
}
.dates li {
  font-size14px;
  margin20px 0
}
.dates li span {
  text-align: center
}
td {
  font-size15px;
  margin20px 0
}

关于怎么在PHP中定义一个page分页类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节
推荐阅读:
  1. php分页类
  2. php 分页类

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×