百度分页函数的实现

Posted on 2015-05-27 00:28  小蕊同学  阅读(317)  评论(0编辑  收藏  举报
$recordcount=$rs[0];//共14条记录
$pagesize=1;//每页2条记录
$pagecount=ceil($recordcount/$pagesize);//总共有几页
$currpage=isset($_GET['p']) ? $_GET['p']:1;//显示当前页

调用函数
  1. <?php include 'inc/db_mysqli.php';?>
  2. <!doctype html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>webrx-php</title>
  7. <?php css1();?> //引用样式
  8. </head>
  9. <body>
  10. <?php
  11. $p = isset($_GET['p']) ? $_GET['p'] : 1;
  12. //(表明,2表示一页显示几行内容,mname字段名,$p表示当前页)
  13. $rs = pager('member',$p,'mname,maccount',2);
  14. $m->close();
  15. echo $rs[1];
  16. foreach($rs[0] as $v){
  17. echo $v[0].'<br>';
  18. }
  19. echo $rs[1];
  20. ?>
  21. </body>
  22. </html>

实现分页的函数
  1. <?php
  2. $host = 'localhost';
  3. $user = 'root';
  4. $pass = '';
  5. $dbname = 'db';
  6. $charset = 'utf8';
  7. $m = new mysqli($host,$user,$pass,$dbname);
  8. $m->set_charset($charset);
  9. function pager($tn,$currpage=1,$f='*',$pagesize=3,$w='1=1'){
  10. global $m;
  11. $stmt = $m->prepare("select count(*) from $tn where $w");
  12. $stmt->execute();
  13. $stmt->bind_result($recordcount);
  14. $stmt->fetch();
  15. $stmt->free_result();
  16. $stmt->close();
  17. $stmt = $m->prepare("select $f from $tn where $w limit ?,?");
  18. $pagecount = ceil($recordcount/$pagesize);
  19. $start = $currpage*$pagesize - $pagesize;
  20. $stmt->bind_param('ii',$start,$pagesize);
  21. $stmt->execute();
  22. $result = $stmt->get_result();
  23. $row = array();
  24. $row[] = $result->fetch_all( MYSQLI_NUM);
  25. $stmt->free_result();
  26. $stmt->close();
  27. $first = 1;
  28. $end = 10;
  29. $pages = '<div class="page">';
  30. if($currpage>=7){
  31. $first = $currpage-5;
  32. $end = $first+$end-1;
  33. }
  34. if($currpage>1){
  35. $prev = $currpage-1;
  36. if($first>1){
  37. $pages.="<a href=?p=1>首页</a><a href=?p=$prev>上一页</a>";
  38. }else{
  39. $pages.="<a href=?p=$prev>上一页</a>";
  40. }
  41. }
  42. for($i=$first;$i<=$end;$i++){
  43. if($i>$pagecount){
  44. break;
  45. }
  46. if($i==$currpage){
  47. $pages.='<a class="checked">'.$i.'</a>';
  48. continue;
  49. }
  50. $pages.="<a href=?p=$i>$i</a>";
  51. }
  52. if($currpage<$pagecount){
  53. $next = $currpage+1;
  54. $pages.="<a href=?p=$next>下一页</a>";
  55. }
  56. if($end<$pagecount){
  57. $pages.="<a href=?p=$pagecount>尾页</a>";
  58. }
  59. $row[] = $pages.'</div>';
  60. $row[] = $pagesize;
  61. $row[] = $pagecount;
  62. $row[] = $recordcount;
  63. $row[] = $currpage;
  64. return $row;
  65. }
  66. function css1(){
  67. $css = <<<css
  68. <style>
  69. .page{font-size:12px;height:30px;padding:15px 0;clear:both;overflow:hidden;text-align:center;}
  70. .page a{text-decoration:none;line-height:25px;padding:0px 10px;display:inline-block;margin-right:5px;border:solid 1px #c8c7c7;}
  71. .page a:hover,.page a.checked{text-decoration:none;border:solid 1px #0086d6;background:#0091e3;color:#fff;}
  72. .page a:visited,.page a:link{color:#333;}
  73. .page a:active{color:#3B3B3B;}
  74. </style>
  75. css;
  76. echo $css;
  77. }





Copyright © 2024 小蕊同学
Powered by .NET 9.0 on Kubernetes