??? ???? ??? PHP ?????? ??
?? ??????? ?? ??? ???, ??? ??, ???? ? ?????? ?????.
??? ???? ??? ??, ?????? ??? ?? ??? ?????, ??? ?? ????? ?????? ??? ??? ?? ????.
?? ?? ????? ??????? ???? ???. ?? ??? ??? ????, ??? ????, ?? ??? ???? ??? ?? ?? ??????. ??? ??? ??? ????? ?? ??? ?? ????.
?? ??? ??? ?? ??? ???? ??? ??? ? ????. ?? ??? ??? ????.
??? ?? ?? config.php? ?? ? ????. ??? ???? ?? ?? ??? ?????. ??? ??? ????.
<?php //數(shù)據(jù)庫服務(wù)器 define('DB_HOST', 'localhost'); //數(shù)據(jù)庫用戶名 define('DB_USER', 'root'); //數(shù)據(jù)庫密碼 define('DB_PWD', 'secret'); //庫名 define('DB_NAME', 'book'); //字符集 define('DB_CHARSET', 'utf8'); ?>
??? ??????? ???? ? ???? ???? ???. Connection.php ??. ??? ??? ????.
<?phpinclude 'config.php';$conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);if (mysqli_errno($conn)) { mysqli_error($conn); exit;}mysqli_set_charset($conn, DB_CHARSET); ?>
?? ? ??? Connection.php ??? ?? ???? ?????? ??? ??? ? ????.
include 'connection.php';
?? ????? ?? ?? ? ???? ??? ???. ??? ?? ??? ??? ????.
????? ?? ?? ??? ????? ???.
元素 | 說明 | 備注 |
---|---|---|
首頁 | 最開始進(jìn)入到頁面的第一頁 | 用get傳參才進(jìn)去時默認(rèn)為1 |
上一頁 | 當(dāng)前頁減1 | 如果頁碼為第一頁時減1,為應(yīng)該為第一頁 |
下一頁 | 當(dāng)前頁加1 | 如果為最后一 |
尾頁 | 最后一頁 | 總條數(shù)除以每頁顯示數(shù)得到總頁數(shù) |
當(dāng)前頁 | 當(dāng)前所在的頁碼 | 就是當(dāng)前的頁碼 |
總頁數(shù) | 一共有多少個頁面 | 總條數(shù)除以每頁顯示數(shù) |
??? ??? ??? ? URL ?? ???? ??? ?? ?? ???? ??? ?? ??? ?????. page.php? ??? ?? ?? ??? ???? ?? ???? ??? ??? ? ????. URL ?? ???? ??? ??? ????.
?? ???? ? ? ?? ??? ???(offset)? ??( num) ??? ?? ??.
?? ???, ??
頁碼 | url中g(shù)et值 | limit偏移量,數(shù)量 |
---|---|---|
第1頁 | 1 | 0,5 |
第2頁 | 2 | 5,5 |
第3頁 | 3 | 10,5 |
第n頁 | n | (n-1)*5,5 |
???? 5?? ??? ????? ?????. ??? ??? ???? ???? ??? ??? ????.
offset的值為 (n-1)*5 num 為規(guī)定的5
??? ?? ????? ?????.
1. ???? ??? ????? ?????.
?? ??
通過查詢user表的count(id),得到總數(shù)$count。 $count_sql = 'select count(id) as c from user'; $result = mysqli_query($conn, $count_sql); $data = mysqli_fetch_assoc($result); //得到總的用戶數(shù) $count = $data['c'];
?? ???
page.php ???? ?? ????? url? http://www.php.com/page ???. .php, ?? no ?page=1 ??? ?? ??? ????.
??? ??? ?? ??? ???? ???? ?? ?? ??? ?? ?? $page? ???? ???.
???? ??? ???? ???? ???? ?? ?? ??((int) $_GET['page'])? ?????.
? ?? ?? ??:
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
? ?? ?? ??
if (isset($_GET['page'])) { $page = (int) $_GET['page']; } else { $page = 1; }
??? ???
? ???? ???? ???. ?? ???? ??? ????. ????? 5.6?? ?? ??? ???? ???. ?? 6??? ???.
???? 20.3???? ??? ??? ?? ???? ???? ???. ??? ??? ?? 21? ?????.
? ??? ?? ? ???? ???? ??? ?? ?? ??? ? ??? ?? ????.
//每頁顯示數(shù) $num = 5; $total = ceil($count / $num);
?? ???, ?? ??? ?? ??
? ????? ?? ???? ????, ??? ????? ?? ???? ???? ??? ????
? ?? ???? ??? ???? ???? ?? ? ???? ???? ????.
??? ??? ??? ??? ???? ???. ??? ??? ?? ? ?? ????? 1? ?? ?? ? ?? ???? ????.
??? ???? ??? ???? ??? ???? ??? ????? ?????.
if ($page <= 1) { $page = 1; } if ($page >= $total) { $page = $total; }
2. SQL ?
?? ???? ??? ???? ??? ?? ? ???? ??? ???? ?????? ?????. SQL ? ??? ????.
?? ?? ??? ??????.
$num = 5; $offset = ($page - 1) * $num;
SQL ?? $num ? $offset? ??????.
$sql = "select id,username,createtime,createip from user order by id desc limit $offset , $num";
Control URI? ??? ?
echo '<tr> <td colspan="5"> <a href="page.php?page=1">首頁</a> <a href="page.php?page=' . ($page - 1) . '">上一頁</a> <a href="page.php?page=' . ($page + 1) . '">下一頁</a> <a href="page.php?page=' . $total . '">尾頁</a> 當(dāng)前是第 ' . $page . '頁 共' . $total . '頁 </td> </tr>';
??? ?? ????? ???? ?? ??? ????.
include 'connection.php'; $count_sql = 'select count(id) as c from user'; $result = mysqli_query($conn, $count_sql); $data = mysqli_fetch_assoc($result); //得到總的用戶數(shù) $count = $data['c']; $page = isset($_GET['page']) ? (int) $_GET['page'] : 1; /* if (isset($_GET['page'])) { $page = (int) $_GET['page']; } else { $page = 1; } */ //每頁顯示數(shù) $num = 5; //得到總頁數(shù) $total = ceil($count / $num); if ($page <= 1) { $page = 1; } if ($page >= $total) { $page = $total; } $offset = ($page - 1) * $num; $sql = "select id,username,createtime,createip from user order by id desc limit $offset , $num"; $result = mysqli_query($conn, $sql); if ($result && mysqli_num_rows($result)) { //存在數(shù)據(jù)則循環(huán)將數(shù)據(jù)顯示出來 echo '<table width="800" border="1">'; while ($row = mysqli_fetch_assoc($result)) { echo '<tr>'; echo '<td>' . $row['username'] . '</td>'; echo '<td>' . date('Y-m-d H:i:s', $row['createtime']) . '</td>'; echo '<td>' . long2ip($row['createip']) . '</td>'; echo '<td><a href="edit.php?id=' . $row['id'] . '">編輯用戶</a></td>'; echo '<td><a href="delete.php?id=' . $row['id'] . '">刪除用戶</a></td>'; echo '</tr>'; } echo '<tr><td colspan="5"><a href="page.php?page=1">首頁</a> <a href="page.php?page=' . ($page - 1) . '">上一頁</a> <a href="page.php?page=' . ($page + 1) . '">下一頁</a> <a href="page.php?page=' . $total . '">尾頁</a> 當(dāng)前是第 ' . $page . '頁 共' . $total . '頁 </td></tr>'; echo '</table>'; } else { echo '沒有數(shù)據(jù)'; } mysqli_close($conn);