百度ページ関数の実現

28491 ワード

$recordcount=$rs[0];//合計14件の記録
$pagesize=1;//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. }