效果圖如下:
代碼如下:
<?php
/**
* Created by PhpStorm.
* User: W&G
* Date: 2017/6/28
* Time: 21:14
*/
/*傳入頁碼*/
$page = $_GET['p'];
/*根據(jù)頁碼取出數(shù)據(jù)php+mysql*/
$host = 'localhost';
$user = 'root';
$pwd = '123456';
$dbname = 'test';
$page_size = 10;
//連接數(shù)據(jù)庫
$conn = @mysql_connect($host,$user,$pwd) or die();
//檢測數(shù)據(jù)庫是否連接成功
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
//選擇數(shù)據(jù)庫
mysql_select_db($dbname, $conn) or die('Can\'t use foo : ' . mysql_error());
//設(shè)置數(shù)據(jù)庫編碼格式
mysql_query('set names utf8');
//準(zhǔn)備SQL語句SELECT * FROM 表名 LIMIT 起始位置剖笙,顯示條數(shù)
$sql = "SELECT * FROM tb_admin_menu LIMIT ". ($page-1)*$page_size . ",$page_size";
//向MySQL服務(wù)發(fā)SQL語句
$result = mysql_query($sql);
//解析處理結(jié)果集
echo '<table border="1" cellspacing="0" width="60%">';
echo '<tr><td>id</td><td>name</td></tr>';
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo '<td>'.$row["Fid"].'</td>';
echo '<td>'.$row["Fmenu"].'</td>';
echo '</tr>';
}
echo '</table>';
//釋放結(jié)果集
mysql_free_result($result);
//獲取數(shù)據(jù)總數(shù)
$total_sql = "SELECT COUNT(*) AS total FROM tb_admin_menu";
$total_result = mysql_fetch_assoc(mysql_query($total_sql));
$total = $total_result['total'];
//計(jì)算總頁數(shù)
$total_pages = ceil($total/$page_size);
//關(guān)閉數(shù)據(jù)庫連接
mysql_close($conn);
/*顯示數(shù)據(jù)并且顯示分頁條*/
$page_banner = '';
if ($page > 1) {
$page_banner = '<a href='.$_SERVER['PHP_SELF']."?p=1".'>首頁</a>';
$page_banner .= '<a href='.$_SERVER['PHP_SELF']."?p=".($page-1).'>上一頁</a>';
}
$page_banner .= '<b>當(dāng)前第'.$page.'頁</b>';
if ($page < $total_pages) {
$page_banner .= '<a href='.$_SERVER['PHP_SELF']."?p=".($page+1).'>下一頁</a>';
$page_banner .= '<a href='.$_SERVER['PHP_SELF']."?p=$total_pages".'>尾頁</a>';
}
$page_banner .= "共{$total_pages}頁<br><br>";
$page_banner .= "<form action='page.php' method='get'>";
$page_banner .= "跳轉(zhuǎn)到第<input type='text' size='2' name='p'>頁";
$page_banner .= "<input type='submit' value='確定'>";
$page_banner .= "</form>";
echo $page_banner;