php分页实例程序函数

在php中分页是我们开发中必须要用到的东西,但在觉得在php分页比在asp中方便了很多,下面我来给各位同学详细介绍人几个不错的php分页类吧,这是我自己写的一个php分页实例,代码如下:

  1. <html>
  2. <head>
  3. <title>简单的PHP分页程序</title>
  4. </head>
  5. <body>
  6. <?php
  7. //建立连接数据库
  8. $linkphpfensi.com","root","") or die("you could notconnect mysql");
  9. //连接的数据库名称
  10. @mysql_select_db("ceshi") or die("could not select database!");
  11. //取得记录总数
  12. $query="SELECT count(*) FROM user";
  13. $rs = mysql_query($query);
  14. $myrows=mysql_fetch_array($rs);
  15. $numrows=$myrows[0];
  16. //设定每一页显示的记录数
  17. $pagesize = 1;
  18. //计算总页数
  19. $pages=intval($numrows/$pagesize);
  20. if($numrows%$pagesize)
  21. $pages++;
  22. //设置页数
  23. if(isset($_POST['page']))
  24. $page=intval($_POST['page']);
  25. else{
  26. $page = 1; //没有页数则显示第一页;
  27. }
  28. //计算记录偏移量
  29. $offset = ($page-1)*$pagesize;
  30. //读取指定的记录数
  31. $query1="select * from user limit $offset,$pagesize";
  32. $rs = mysql_query($query1);
  33. if($myrows=mysql_fetch_array($rs))
  34. {
  35. $i = 0;
  36. ?>
  37. <table width="80%" >
  38. <tr>
  39. <td width="50%">用户名</td>
  40. <td width="50%">密码</td>
  41. </tr>
  42. <?php
  43. do{
  44. $i++;
  45. ?>
  46. <tr>
  47. <td width="50%"><?php echo $myrows["username"] ?></td>
  48. <td width="50%"><?php echo $myrows["password"] ?></td>
  49. </tr>
  50. <?php
  51. }
  52. while($myrows=mysql_fetch_array($rs));
  53. echo "</table>";
  54. }
  55. $first = 1;
  56. $prev =$page-1;
  57. $next = $page+1;
  58. $last=$pages;
  59. if($page>1)
  60. {
  61. echo "<a href='fenye.php?page=".$first."'>首页</a>";
  62. echo "<a href='fenye.php?page=".$prev."'>上一页</a>";
  63. }
  64. if($page<$pages)
  65. {
  66. echo "<a href='fenye.php?page=".$next."'>下一页</a>";
  67. echo "<a href='fenye.php?page=".$last."'>最后一页</a>";
  68. }
  69. echo "<div align = 'center'>共有" .$pages. "页(" .$page. "/" .$pages.")";
  70. for($i=1; $i<$page; $i++)
  71. echo "<a href='fenye.php?page=".$i."'>[".$i."]</a>";
  72. echo "[" .$page. "]";
  73. for($i=$page+1; $i<=$pages;$i++)
  74. echo "<a href='fenye.php?page=".$i."'>[".$i."]</a>";
  75. echo"</div>";
  76. ?>
  77. </body>
  78. </html>

上面代码不能重复使用,后来整理了一个类,CSS代码如下:

  1. #pages{display:block; text-align:center; overflow:hidden; color:#000; font-size:13px}
  2. #pages a{color:#333; text-decoration:none; font-family:Verdana,Geneva,sans-serif; padding:2px 3px; display:block}
  3. #pages li{float:left; display:inline-block; border:1px solid #999; margin-right:3px; margin-left:3px}
  4. #pages #spages{background:#CCC; font-weight:bold; color:#36C}

PHP分页函数类源码如下:

  1. <?php
  2. /*
  3. * 分页模块
  4. */
  5. interface Page{
  6. public function showpage();
  7. }
  8. class AdminPage implements Page{
  9. /*
  10. * 构造参数
  11. * SQL语句,每页显示数,url
  12. */
  13. private $sql;
  14. private $pageline; //每页显示多少行数据
  15. private $urlstr;
  16. private $nowpage;
  17. private $totalNum;
  18. private $pageNum;
  19. private $pageStr;// 形成分页字符串
  20. public function __construct($sql,$p){
  21. $this->sql = $sql;
  22. $this->pageline = $p;
  23. $this->urlstr = $_SERVER['SCRIPT_NAME'];
  24. if(emptyempty($_GET['p'])){
  25. $this->nowpage = 1; //初始化当前页数为1
  26. }else if(is_numeric($_GET['p'])){
  27. $this->nowpage = $_GET['p'];
  28. }else{
  29. $e = new Error(6);
  30. $e->show();
  31. }
  32. $s = new Sql();
  33. $this->totalNum = $s->results_exist_num($this->sql);
  34. $this->pageNum = ceil($this->totalNum/$this->pageline);
  35. }
  36. /*
  37. * 形式为:
  38. * 首页| 上一页| 1 | 2 | 3 | 4 | 5 |下一页 | 末页
  39. *
  40. *
  41. */
  42. public function showpage(){
  43. if($this->pageNum >=1 && $this->nowpage <5 ){
  44. if( $this->pageNum <=5){
  45. $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
  46. if($this->nowpage!=1){
  47. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
  48. }
  49. for($i=1;$i<=$this->pageNum;$i++){
  50. if($i==$this->nowpage){
  51. $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  52. }else{
  53. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  54. }
  55. }
  56. if($this->nowpage !=$this->pageNum){
  57. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
  58. }
  59. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
  60. echo $this->pageStr;
  61. }else if($this->pageNum>5){
  62. $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
  63. if($this->nowpage !=1){
  64. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
  65. }
  66. for($i=1;$i<=5;$i++){
  67. if($i==$this->nowpage){
  68. $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  69. }else{
  70. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  71. }
  72. }
  73. if($this->nowpage !=$this->pageNum){
  74. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
  75. }
  76. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
  77. echo $this->pageStr;
  78. }
  79. }else if($this->nowpage <= $this->pageNum -2 && $this->nowpage>=5){
  80. $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
  81. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
  82. for($i=$this->nowpage-2;$i<=$this->nowpage+2;$i++){
  83. if($i==$this->nowpage){
  84. $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  85. }else{
  86. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  87. }
  88. }
  89. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
  90. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
  91. echo $this->pageStr;
  92. }else if($this->nowpage>=$this->pageNum-3 && $this->nowpage <=$this->pageNum){
  93. $this->pageStr = '<ul ><li><a href="'.$this->urlstr.'?p=1" target="rframe" >首页</a></li>';
  94. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage-1).'" target="rframe" >上一页</a></li>';
  95. for($i=$this->pageNum-4;$i<=$this->pageNum;$i++){
  96. if($i==$this->nowpage){
  97. $this->pageStr .='<li ><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  98. }else{
  99. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$i.'" target="rframe" >'.$i.'</a></li>';
  100. }
  101. }
  102. if($this->nowpage !=$this->pageNum){
  103. $this->pageStr .= '<li><a href="'.$this->urlstr.'?p='.($this->nowpage+1).'" target="rframe" >下一页</a></li>';
  104. }
  105. $this->pageStr .='<li><a href="'.$this->urlstr.'?p='.$this->pageNum.'" target="rframe" >末页</a></li>';
  106. echo $this->pageStr;
  107. }
  108. }
  109. }