经典php分页代码,支持数字偏移

这款php分页代码是一款完整的php mysql css的经典分页功能,很适合于大分类有像phpwindow dz等论坛效果.

CSS样式代码如下:

  1. <style type="text/css">
  2. <!--
  3. body {
  4. width:800px;
  5. margin:0 auto;
  6. margin-top:50px;
  7. font-size:12px;
  8. }
  9. a {
  10. color:#014ccc;
  11. text-decoration:none;
  12. }
  13. .div {
  14. float:left;
  15. cursor:pointer;
  16. font-weight:bold;
  17. margin-right:5px;
  18. display: block;
  19. padding:3px 7px;
  20. text-align:center;
  21. border:#bbdded solid 1px;
  22. }
  23. .divs {
  24. float:left;
  25. font-weight:bold;
  26. margin-right:5px;
  27. display: block;
  28. padding:3px 7px;
  29. text-align:center;
  30. border:#cccccc solid 1px;
  31. }
  32. .divsi {
  33. float:left;
  34. font-weight:bold;
  35. margin-right:5px;
  36. display: block;
  37. padding:3px 7px;
  38. text-align:center;
  39. background:#3399ff;
  40. color:#ffffff;
  41. border:#cccccc solid 1px;
  42. }
  43. .div:hover {
  44. background:#3399ff;
  45. color:#ffffff;
  46. }
  47. .divsi:hover {
  48. background:#3399ff;
  49. color:#ffffff;
  50. }
  51. -->
  52. </style>

PHP实例效果代码如下:

  1. <?php
  2. mysql_connect('localhost','root','');
  3. mysql_select_db('news');
  4. mysql_query('set names gbk');
  5. $sql = mysql_query("select * from `v_char`");
  6. $num = mysql_num_rows($sql);
  7. include 'page.class.php';
  8. /*参数说明:
  9. *getpages($pagesize,$pagenum,$page,$init,$page_len);
  10. *$pagesize 每页记录
  11. *$pagenum 总记录数
  12. *$page $_get[page]
  13. *$init 数字分页最小数,默认为1,一般不用更改。
  14. *$page_len 数字分页的数量。
  15. */
  16. $model = new getpages(10,$num,$_get['page']);
  17. $sql = "select * from `v_char` limit $model->offset,$model->pagesize";
  18. $res = mysql_query($sql);
  19. while($row = mysql_fetch_assoc($res))
  20. {
  21. echo $row['id'] . " - " . $row['name'] .'<br>';
  22. }
  23. //输出分页信息
  24. echo $model->getpage();
  25. ?>
  26. page.class.php
  27. <?php
  28. /*
  29. * author:陈凯
  30. * data:2010-09-14
  31. */
  32. class getpages
  33. {
  34. public $pagesize; //每页记录
  35. public $offset; //起始记录数
  36. public $pagenum; //总记录
  37. public $page; //当前页码
  38. public $init; //数字分页最左边数字
  39. public $page_len; //数字分页最右边数字
  40. public $pages; //总页数
  41. //初始化赋值。
  42. function __construct($pagesize,$pagenum,$page,$init=1,$page_len=7)
  43. {
  44. $this->pagesize = $pagesize;//每页记录
  45. $this->pagenum = $pagenum; //总记录
  46. $this->page = $page;
  47. $this->pages = ceil($this->pagenum/$this->pagesize); //总页数
  48. if(emptyempty($this->page) || $this->page <1 || $this->page > $this->pages)$this->page=1;
  49. $this->init = $init; //数字分页左
  50. $this->page_len = $page_len;//数字分页右
  51. $this->offset = ($this->page-1)*$this->pagesize;
  52. }
  53. //输出分页
  54. function getpage()
  55. {
  56. $this->prepg = $this->page-1; //上一页
  57. $this->nextpg= ($this->page == $this->pages) ? 0 : $this->page+1; //下一页
  58. // if($this->pages == 1) return false; //如果只有一页跳出
  59. $pagenav = "<span class='divs'>共 ".$this->pages."页".$this->pagenum."条</span>";
  60. if($this->page == 1)
  61. {
  62. $pagenav .="<a href='#' class='div'>首页</a> ";
  63. $pagenav .="<a href='#' class='div'>上一页</a> ";
  64. }
  65. else
  66. {
  67. $pagenav .="<a href='".$this->get_url()."=1' class='div'>首页</a> ";
  68. $pagenav .="<a href='".$this->get_url()."=".$this->prepg."' class='div'>上一页</a> ";
  69. }
  70. $pagenav .= $this->getvalpage(); //数字分页
  71. if($this->page == $this->pages)
  72. {
  73. $pagenav .="<a href='#' class='div'>下一页</a> ";
  74. $pagenav .="<a href='#' class='div'>尾页</a> ";
  75. }
  76. else
  77. {
  78. $pagenav .="<a href='".$this->get_url()."=".$this->nextpg."' class='div'>下一页</a> ";
  79. $pagenav .="<a href='".$this->get_url()."=".$this->pages."' class='div'>尾页</a> ";
  80. }//开源代码phpfensi.com
  81. //下拉菜单分页列表
  82. 代码如下 复制代码
  83. $pagenav .= "<select name='page' class='select' onchange='window.location.href="".$this->get_url()."="+this.value'> ";
  84. for($i=1;$i<=$this->pages;$i++)
  85. {
  86. if($i == $this->page){
  87. $pagenav .= "<option value='".$i."' selected>".$i."</option> ";
  88. }
  89. else
  90. {
  91. $pagenav .= "<option value='".$i."'>".$i."</option> ";
  92. }
  93. }
  94. $pagenav.="</select> ";
  95. return $pagenav;
  96. }
  97. //数字分页
  98. function getvalpage()
  99. {
  100. $max_p = $this->pages;
  101. $this->page_len = ($this->page_len%2) ? $this->page_len : $this->page_len+1; //如果为偶数便+1
  102. $pageoffset = ($this->page_len-1)/2; //数字分页左右数量
  103. //总页数和数字分页显示数
  104. if($this->pages > $this->page_len)
  105. {
  106. //数字分页左右数量大于等于当前页面,不进行偏移
  107. if($pageoffset>=$this->page)
  108. {
  109. $this->init = 1;
  110. $max_p= $this->page_len;
  111. }
  112. else
  113. {
  114. //当前页码+左右数量 大于总数量,不进行偏移
  115. if($this->page+$pageoffset > $this->pages)
  116. {
  117. $this->init = $this->pages-$this->page_len+1;
  118. }
  119. else
  120. {
  121. //进行偏移,最左边 当前页码 - 左右数量
  122. $this->init = $this->page-$pageoffset;
  123. $max_p = $this->page+$pageoffset;
  124. }
  125. }
  126. }
  127. //循环数字分页
  128. for($i=$this->init ;$i<=$max_p;$i++)
  129. {
  130. //$i 等于 当前页码
  131. if($i == $this->page)
  132. {
  133. $pagenav .= "<a href='#' class='divsi'>".$i."</a>";
  134. }
  135. else
  136. {
  137. $pagenav .= "<a href='".$this->get_url()."=".$i."' class='div'>".$i."</a>";
  138. }
  139. }
  140. return $pagenav;
  141. }
  142. //获取url地址
  143. function get_url()
  144. {
  145. $url = $_server['request_uri'];
  146. $parse_url = parse_url($url);
  147. $query_url = $parse_url['query']; //取出url上的参数
  148. if($query_url)
  149. {
  150. //将&page 替换为空
  151. $query_url = ereg_replace("(^|&)page=$this->page","",$query_url);
  152. $url = str_replace($parse_url['query'],$query_url,$url);
  153. if($query_url)$url .= '&page';
  154. else $url .= 'page';
  155. }
  156. else
  157. {
  158. $url .= '?page';
  159. }
  160. return $url;
  161. }
  162. }
  163. ?>

数据库代码如下:

  1. /*
  2. navicat mysql data transfer
  3. source server : localhost_3306
  4. source server version : 50051
  5. source host : localhost:3306
  6. source database : news
  7. target server type : mysql
  8. target server version : 50051
  9. file encoding : 65001
  10. date: 2010-03-30 00:17:08
  11. */
  12. set foreign_key_checks=0;
  13. -- ----------------------------
  14. -- table structure for `v_char`
  15. -- ----------------------------
  16. drop table if exists `v_char`;
  17. create table `v_char` (
  18. `id` int(10) not null auto_increment,
  19. `name` char(10) default null,
  20. primary key (`id`)
  21. ) engine=innodb auto_increment=401 default charset=gbk;
  22. -- ----------------------------
  23. -- records of v_char
  24. -- ----------------------------
  25. insert into `v_char` values ('1', 'test1');
  26. insert into `v_char` values ('2', 'test2');
  27. insert into `v_char` values ('3', 'test3');
  28. insert into `v_char` values ('4', 'test4');
  29. insert into `v_char` values ('5', 'test5');
  30. insert into `v_char` values ('6', 'test6');
  31. insert into `v_char` values ('7', 'test7');
  32. insert into `v_char` values ('8', 'test8');
  33. insert into `v_char` values ('9', 'test9');
  34. insert into `v_char` values ('10', 'test10');
  35. insert into `v_char` values ('11', 'test11');
  36. insert into `v_char` values ('12', 'test12');