1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> 4 <style> 5 body{font-size:14px;font-family:verdana; } 6 table{text-align: center;margin: 50px auto auto } 7 table tr td{padding: 4px 15px} 8 table thead tr th{padding: 6px 15px} 9 table tbody tr:nth-child(2n){ background:#D8F2FF;} 10 div.pageBox{text-align: center; } 11 div.pageBox a{border:1px solid #444;text-decoration: none;padding: 2px 5px 2px 5px;margin:0 5px ;color: #444; } 12 div.pageBox span.current{border: 1px solid #3D7ABB; background:#3D7ABB;padding: 4px 6px 4px 6px;margin: 2px; color: white;font-weight: 700} 13 div.pageBox span.disable{border: 1px solid #E6E6E6; border-radius: 3px; padding: 2px 5px 2px 5px; margin: 2px; color: #A9A9A9; background: #E6E6E6;} 14 div.pageBox form{display: inline;} 15 </style> 16 </head> 17 18 <body> 19 <?php 20 // +---------------------------------------------------------------------- 21 // | EBACERA [ WE CAN DO IT JUST THINK IT ] 22 // +---------------------------------------------------------------------- 23 // | Copyright (c) 2016 http://www.ebacera.com All rights reserved. 24 // +---------------------------------------------------------------------- 25 // | Author: 重设人生 <573914456@qq.com> 26 // +---------------------------------------------------------------------- 27 // 分页原理 28 // 1、传入页码 29 // 2、根据页码取出数据 30 // 3、显示数据和页码条 31 32 header("Content-type:text/html;charset=utf-8"); 33 34 //连接数据库 35 $con = @mysql_connect("localhost", 'root', '123456'); 36 if (!$con) { 37 die("数据库连接失败!"); 38 } 39 //传入页码 40 $page = isset($_GET['p']) ? $_GET['p'] : 0; 41 //显示几页 42 $pageSize = 10; 43 //总页数 44 $pageCount = 0; 45 //总记录数 46 $totalNum = 0; 47 //显示几个页码左右就用...省略 48 $showPage = 5; 49 // 50 if ($page <= 0) { 51 $page = 1; 52 } 53 54 //选择数据库 55 mysql_select_db("studymysql", $con); 56 //设置编码格式 57 mysql_query("set names utf8"); 58 $count = mysql_fetch_row(mysql_query("select count(*) from admin")); 59 //获取总页数 60 $pageCount = $count[0]; 61 //共有多少页【ceil() 函数向上舍入为最接近的整数。】 62 $totalNum = ceil($pageCount / $pageSize); 63 if($page > $totalNum){ 64 $page =$totalNum; 65 } 66 //sql 67 $sql = "select * from admin order by user_id asc limit " . ($page - 1) * $pageSize . ",{$pageSize} "; 68 //获取资源 69 $res = mysql_query($sql); 70 echo "<table border='1' style='border-collapse: collapse' align='center' width='40%'> <thead style='background: #C3C3C3;'><tr><th>编号</th><th>名字</th></tr></thead> <tbody>"; 71 //遍历数据 72 while ($row = mysql_fetch_assoc($res)) { 73 echo "<tr><td>{$row['user_id']}</td><td>{$row['user_name']}</td></tr>"; 74 } 75 echo "</tbody></table>"; 76 //关闭资源 77 mysql_free_result($res); 78 //存放分页条 79 $page_banner = '<div class="pageBox">'; 80 //显示数据+分页条 81 //计算偏移量 82 $pageOffset = ($showPage - 1) / 2; 83 84 85 //如果当前页大于1 86 if ($page > 1) { 87 //首页 88 $page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=1'>首页</a>"; 89 //上一页 90 $page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . ($page - 1) . "'><上一页</a> "; 91 }else{ 92 $page_banner .= "<span class='disable'>首页</span>"; 93 $page_banner .= "<span class='disable'>上一页</span>"; 94 95 } 96 97 //初始化数据 98 $star = 1; 99 $end = $totalNum; 100 // 101 echo $showPage; 102 if ($totalNum > $showPage) { 103 104 if ($page > $pageOffset + 1) { 105 $page_banner .= "..."; 106 } 107 108 if ($page > $pageOffset) { 109 echo "-----------"; 110 $star = $page - $pageOffset; 111 echo $star; 112 $end = $totalNum > $page + $pageOffset ? $page + $pageOffset : $totalNum; 113 } else { 114 115 $star = 1; 116 $end = $totalNum > $showPage ? $showPage : $totalNum; 117 118 } 119 120 if ($page + $pageOffset > $totalNum) { 121 $star = $star - ($page + $pageOffset - $end); 122 } 123 124 } 125 126 127 //---------------------------- 128 for ($i = $star; $i <= $end; $i++) { 129 if($page == $i){ 130 $page_banner .= "<span class='current'>{$i}</span>"; 131 }else{ 132 $page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p={$i}'>{$i}</a>"; 133 } 134 } 135 //---------------------------- 136 137 138 if ($totalNum > $showPage && $totalNum > $page + $pageOffset) { 139 $page_banner .= "..."; 140 } 141 142 143 //如果当前页小于最后一页 144 if ($page < $totalNum) { 145 //下一页 146 $page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . ($page + 1) . "'>下一页></a>"; 147 //尾页 148 $page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . ($totalNum) . "'>尾页</a>"; 149 }else{ 150 $page_banner .= "<span class='disable'>下一页</span>"; 151 $page_banner .= "<span class='disable'>尾页</span>"; 152 } 153 154 //总页数 155 $page_banner .= " 【共{$totalNum}页】"; 156 $page_banner .= "<form action='" . $_SERVER['PHP_SELF'] . "' method='get' >"; 157 $page_banner .= " 到第 <input type='text' name='p' value='' size='2'/> <input type='submit' /></form>"; 158 $page_banner .= "</form>"; 159 $page_banner .= "</div>"; 160 161 echo "<br/><br/>"; 162 echo $page_banner; 163 164 //关闭资源 165 mysql_close($con); 166 167 168 //格式化输出 169 function show_dump($arr) 170 { 171 echo "<pre>"; 172 print_r($arr); 173 echo "</pre>"; 174 } 175 176 ?> 177 178 </body> 179 </html>