PHP分页

index.php

php版本5以上使用mysqli

  1. <?php
  2. include_once("config.php");
  3. require_once('page.class.php'); //分页类
  4. $showrow = 10; //一页显示的行数
  5. $curpage = empty($_GET['page']) ? 1 : $_GET['page']; //当前的页,还应该处理非数字的情况
  6. $url = "?page={page}"; //分页地址,如果有检索条件 ="?page={page}&q=".$_GET['q']
  7. //省略了链接mysql的代码,测试时自行添加
  8. //$sql = "SELECT * FROM data_type";
  9. $sql = "SELECT * FROM data_type";
  10. $total = mysqli_num_rows(mysqli_query($conn, $sql)); //记录总条数
  11. if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow))
  12. $curpage = ceil($total_rows / $showrow); //当前页数大于最后页数,取最后一页
  13. //获取数据
  14. $sql .= " LIMIT " . ($curpage - 1) * $showrow . ",$showrow;";
  15. $query = mysqli_query($conn, $sql);
  16. ?>
  17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  18. <html xmlns="http://www.w3.org/1999/xhtml">
  19. <head>
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  21. <title>演示:PHP简单漂亮的分页类</title>
  22. <meta name="keywords" content="php分页类" />
  23. <meta name="description" content="本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap。" />
  24. <link rel="stylesheet" type="text/css" href="http://www.sucaihuo.com/jquery/css/common.css" />
  25. <style type="text/css">
  26. p{margin:0}
  27. #page{
  28. height:40px;
  29. padding:20px 0px;
  30. }
  31. #page a{
  32. display:block;
  33. float:left;
  34. margin-right:10px;
  35. padding:2px 12px;
  36. height:24px;
  37. border:1px #cccccc solid;
  38. background:#fff;
  39. text-decoration:none;
  40. color:#808080;
  41. font-size:12px;
  42. line-height:24px;
  43. }
  44. #page a:hover{
  45. color:#077ee3;
  46. border:1px #077ee3 solid;
  47. }
  48. #page a.cur{
  49. border:none;
  50. background:#077ee3;
  51. color:#fff;
  52. }
  53. #page p{
  54. float:left;
  55. padding:2px 12px;
  56. font-size:12px;
  57. height:24px;
  58. line-height:24px;
  59. color:#bbb;
  60. border:1px #ccc solid;
  61. background:#fcfcfc;
  62. margin-right:8px;
  63. }
  64. #page p.pageRemark{
  65. border-style:none;
  66. background:none;
  67. margin-right:0px;
  68. padding:4px 0px;
  69. color:#666;
  70. }
  71. #page p.pageRemark b{
  72. color:red;
  73. }
  74. #page p.pageEllipsis{
  75. border-style:none;
  76. background:none;
  77. padding:4px 0px;
  78. color:#808080;
  79. }
  80. .dates li {font-size: 14px;margin:20px 0}
  81. .dates li span{float:right}
  82. </style>
  83. </head>
  84. <body>
  85. <div class="head">
  86. <div class="head_inner clearfix">
  87. <ul id="nav">
  88. <li><a href="http://www.sucaihuo.com">首 页</a></li>
  89. <li><a href="http://www.sucaihuo.com/templates">网站模板</a></li>
  90. <li><a href="http://www.sucaihuo.com/js">网页特效</a></li>
  91. <li><a href="http://www.sucaihuo.com/php">PHP</a></li>
  92. <li><a href="http://www.sucaihuo.com/site">精选网址</a></li>
  93. </ul>
  94. <a class="logo" href="http://www.sucaihuo.com"><img src="http://www.sucaihuo.com/Public/images/logo.jpg" alt="素材火logo" /></a>
  95. </div>
  96. </div>
  97. <div class="container">
  98. <div class="demo">
  99. <h2 class="title"><a href="http://www.sucaihuo.com/php/223.html">教程:PHP简单漂亮的分页类</a></h2>
  100. <div class="showData">
  101. <ul class="dates">
  102. <?php while($row = mysqli_fetch_assoc($query)) { ?>
  103. <li>
  104. <span><?php echo $row['t3'] ?></span>
  105. <a target="_blank" href="http://www.sucaihuo.com/js"><?php echo $row['t1'] ?></a>
  106. </li>
  107. <?php } ?>
  108. </ul>
  109. <!--显示数据区-->
  110. </div>
  111. <div class="showPage">
  112. <?php
  113. if ($total > $showrow) {//总记录数大于每页显示数,显示分页
  114. $page = new page($total, $showrow, $curpage, $url, 2);
  115. echo $page->myde_write();
  116. }
  117. ?>
  118. </div>
  119. </div>
  120. </div>
  121. <div class="foot">
  122. Powered by sucaihuo.com 本站皆为作者原创,转载请注明原文链接:<a href="http://www.sucaihuo.com" target="_blank">www.sucaihuo.com</a>
  123. </div>
  124. <script type="text/javascript" src="http://www.sucaihuo.com/Public/js/other/jquery.js"></script>
  125. </body>
  126. </html>


php版本5以下用这个:

  1. <?php
  2. include_once("config.php");
  3. require_once('page.class.php'); //分页类
  4. $showrow = 10; //一页显示的行数
  5. $curpage = empty($_GET['page']) ? 1 : $_GET['page']; //当前的页,还应该处理非数字的情况
  6. $url = "?page={page}"; //分页地址,如果有检索条件 ="?page={page}&q=".$_GET['q']
  7. //省略了链接mysql的代码,测试时自行添加
  8. $sql = "SELECT * FROM userinfo";
  9. $total = mysql_num_rows(mysql_query($sql)); //记录总条数
  10. if (!empty($_GET['page']) && $total != 0 && $curpage > ceil($total / $showrow))
  11. $curpage = ceil($total_rows / $showrow); //当前页数大于最后页数,取最后一页
  12. //获取数据
  13. $sql .= " LIMIT " . ($curpage - 1) * $showrow . ",$showrow;";
  14. $query = mysql_query($sql);
  15. ?>
  16. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  17. <html xmlns="http://www.w3.org/1999/xhtml">
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  20. <title>演示:PHP简单漂亮的分页类</title>
  21. <meta name="keywords" content="php分页类" />
  22. <meta name="description" content="本文介绍一款原生的PHP分页类,分页样式有点类似bootstrap。" />
  23. <link rel="stylesheet" type="text/css" href="http://www.sucaihuo.com/jquery/css/common.css" />
  24. <style type="text/css">
  25. p{margin:0}
  26. #page{
  27. height:40px;
  28. padding:20px 0px;
  29. }
  30. #page a{
  31. display:block;
  32. float:left;
  33. margin-right:10px;
  34. padding:2px 12px;
  35. height:24px;
  36. border:1px #cccccc solid;
  37. background:#fff;
  38. text-decoration:none;
  39. color:#808080;
  40. font-size:12px;
  41. line-height:24px;
  42. }
  43. #page a:hover{
  44. color:#077ee3;
  45. border:1px #077ee3 solid;
  46. }
  47. #page a.cur{
  48. border:none;
  49. background:#077ee3;
  50. color:#fff;
  51. }
  52. #page p{
  53. float:left;
  54. padding:2px 12px;
  55. font-size:12px;
  56. height:24px;
  57. line-height:24px;
  58. color:#bbb;
  59. border:1px #ccc solid;
  60. background:#fcfcfc;
  61. margin-right:8px;
  62. }
  63. #page p.pageRemark{
  64. border-style:none;
  65. background:none;
  66. margin-right:0px;
  67. padding:4px 0px;
  68. color:#666;
  69. }
  70. #page p.pageRemark b{
  71. color:red;
  72. }
  73. #page p.pageEllipsis{
  74. border-style:none;
  75. background:none;
  76. padding:4px 0px;
  77. color:#808080;
  78. }
  79. .dates li {font-size: 14px;margin:20px 0}
  80. .dates li span{float:right}
  81. </style>
  82. </head>
  83. <body>
  84. <div class="head">
  85. <div class="head_inner clearfix">
  86. <ul id="nav">
  87. <li><a href="http://www.sucaihuo.com">首 页</a></li>
  88. <li><a href="http://www.sucaihuo.com/templates">网站模板</a></li>
  89. <li><a href="http://www.sucaihuo.com/js">网页特效</a></li>
  90. <li><a href="http://www.sucaihuo.com/php">PHP</a></li>
  91. <li><a href="http://www.sucaihuo.com/site">精选网址</a></li>
  92. </ul>
  93. <a class="logo" href="http://www.sucaihuo.com"><img src="http://www.sucaihuo.com/Public/images/logo.jpg" alt="素材火logo" /></a>
  94. </div>
  95. </div>
  96. <div class="container">
  97. <div class="demo">
  98. <h2 class="title"><a href="http://www.sucaihuo.com/php/223.html">教程:PHP简单漂亮的分页类</a></h2>
  99. <div class="showData">
  100. <ul class="dates">
  101. <?php while ($row = mysql_fetch_array($query)) { ?>
  102. <li>
  103. <span><?php echo $row['t3'] ?></span>
  104. <a target="_blank" href="http://www.sucaihuo.com/js"><?php echo $row['t1'] ?></a>
  105. </li>
  106. <?php } ?>
  107. </ul>
  108. <!--显示数据区-->
  109. </div>
  110. <div class="showPage">
  111. <?php
  112. if ($total > $showrow) {//总记录数大于每页显示数,显示分页
  113. $page = new page($total, $showrow, $curpage, $url, 2);
  114. echo $page->myde_write();
  115. }
  116. ?>
  117. </div>
  118. </div>
  119. </div>
  120. <div class="foot">
  121. Powered by sucaihuo.com 本站皆为作者原创,转载请注明原文链接:<a href="http://www.sucaihuo.com" target="_blank">www.sucaihuo.com</a>
  122. </div>
  123. <script type="text/javascript" src="http://www.sucaihuo.com/Public/js/other/jquery.js"></script>
  124. </body>
  125. </html>

page.class.php代码

  1. <?php
  2. /* * *********************************************
  3. * @类名: page
  4. * @参数: $myde_total - 总记录数
  5. * $myde_size - 一页显示的记录数
  6. * $myde_page - 当前页
  7. * $myde_url - 获取当前的url
  8. * @功能: 分页实现
  9. * @作者: 宋海阁
  10. */
  11. class page {
  12. private $myde_total; //总记录数
  13. private $myde_size; //一页显示的记录数
  14. private $myde_page; //当前页
  15. private $myde_page_count; //总页数
  16. private $myde_i; //起头页数
  17. private $myde_en; //结尾页数
  18. private $myde_url; //获取当前的url
  19. /*
  20. * $show_pages
  21. * 页面显示的格式,显示链接的页数为2*$show_pages+1。
  22. * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
  23. */
  24. private $show_pages;
  25. public function __construct($myde_total = 1, $myde_size = 1, $myde_page = 1, $myde_url, $show_pages = 2) {
  26. $this->myde_total = $this->numeric($myde_total);
  27. $this->myde_size = $this->numeric($myde_size);
  28. $this->myde_page = $this->numeric($myde_page);
  29. $this->myde_page_count = ceil($this->myde_total / $this->myde_size);
  30. $this->myde_url = $myde_url;
  31. if ($this->myde_total < 0)
  32. $this->myde_total = 0;
  33. if ($this->myde_page < 1)
  34. $this->myde_page = 1;
  35. if ($this->myde_page_count < 1)
  36. $this->myde_page_count = 1;
  37. if ($this->myde_page > $this->myde_page_count)
  38. $this->myde_page = $this->myde_page_count;
  39. $this->limit = ($this->myde_page - 1) * $this->myde_size;
  40. $this->myde_i = $this->myde_page - $show_pages;
  41. $this->myde_en = $this->myde_page + $show_pages;
  42. if ($this->myde_i < 1) {
  43. $this->myde_en = $this->myde_en + (1 - $this->myde_i);
  44. $this->myde_i = 1;
  45. }
  46. if ($this->myde_en > $this->myde_page_count) {
  47. $this->myde_i = $this->myde_i - ($this->myde_en - $this->myde_page_count);
  48. $this->myde_en = $this->myde_page_count;
  49. }
  50. if ($this->myde_i < 1)
  51. $this->myde_i = 1;
  52. }
  53. //检测是否为数字
  54. private function numeric($num) {
  55. if (strlen($num)) {
  56. if (!preg_match("/^[0-9]+$/", $num)) {
  57. $num = 1;
  58. } else {
  59. $num = substr($num, 0, 11);
  60. }
  61. } else {
  62. $num = 1;
  63. }
  64. return $num;
  65. }
  66. //地址替换
  67. private function page_replace($page) {
  68. return str_replace("{page}", $page, $this->myde_url);
  69. }
  70. //首页
  71. private function myde_home() {
  72. if ($this->myde_page != 1) {
  73. return "<a href='" . $this->page_replace(1) . "' title='首页'>首页</a>";
  74. } else {
  75. return "<p>首页</p>";
  76. }
  77. }
  78. //上一页
  79. private function myde_prev() {
  80. if ($this->myde_page != 1) {
  81. return "<a href='" . $this->page_replace($this->myde_page - 1) . "' title='上一页'>上一页</a>";
  82. } else {
  83. return "<p>上一页</p>";
  84. }
  85. }
  86. //下一页
  87. private function myde_next() {
  88. if ($this->myde_page != $this->myde_page_count) {
  89. return "<a href='" . $this->page_replace($this->myde_page + 1) . "' title='下一页'>下一页</a>";
  90. } else {
  91. return"<p>下一页</p>";
  92. }
  93. }
  94. //尾页
  95. private function myde_last() {
  96. if ($this->myde_page != $this->myde_page_count) {
  97. return "<a href='" . $this->page_replace($this->myde_page_count) . "' title='尾页'>尾页</a>";
  98. } else {
  99. return "<p>尾页</p>";
  100. }
  101. }
  102. //输出
  103. public function myde_write($id = 'page') {
  104. $str = "<div id=" . $id . ">";
  105. $str.=$this->myde_home();
  106. $str.=$this->myde_prev();
  107. if ($this->myde_i > 1) {
  108. $str.="<p class='pageEllipsis'>...</p>";
  109. }
  110. for ($i = $this->myde_i; $i <= $this->myde_en; $i++) {
  111. if ($i == $this->myde_page) {
  112. $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页' class='cur'>$i</a>";
  113. } else {
  114. $str.="<a href='" . $this->page_replace($i) . "' title='第" . $i . "页'>$i</a>";
  115. }
  116. }
  117. if ($this->myde_en < $this->myde_page_count) {
  118. $str.="<p class='pageEllipsis'>...</p>";
  119. }
  120. $str.=$this->myde_next();
  121. $str.=$this->myde_last();
  122. $str.="<p class='pageRemark'>共<b>" . $this->myde_page_count .
  123. "</b>页<b>" . $this->myde_total . "</b>条数据</p>";
  124. $str.="</div>";
  125. return $str;
  126. }
  127. }
  128. ?>



config.php代码

  1. <?php
  2. $host="localhost";
  3. $db_user="root";
  4. $db_pass="root";
  5. $db_name="job";
  6. $timezone="Asia/Shanghai";
  7. $link=mysql_connect($host,$db_user,$db_pass);
  8. mysql_select_db($db_name,$link);
  9. mysql_query("SET names UTF8");
  10. header("Content-Type: text/html; charset=utf-8");
  11. ?>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值