MySQL+php實現(xiàn)分頁
嵩明網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,嵩明網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為嵩明上1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的嵩明做網(wǎng)站的公司定做!
數(shù)據(jù)庫設(shè)計,如圖
/**
* 1.獲取頁數(shù)
*/
$page = empty($_GET['p'])?1:($_GET['p']);
/**
* 2.處理數(shù)據(jù)
*/
// 鏈接數(shù)據(jù)庫
$host = "127.0.0.1";
$user = "root";
$pwd = "root";
$db = "test";
$mysqli = mysqli_connect($host $user $pwd $db);
if (!$mysqli) {
echo "數(shù)據(jù)庫連接錯誤";
exit();
}
// 設(shè)置參數(shù)
$pageSize = 10;
// 查詢并顯示數(shù)據(jù)(分頁公式:(當前頁-1)*每頁顯示的條數(shù) 每頁顯示的條數(shù))
$sql = "select * from page limit ".($page-1)*$pageSize.""."{$pageSize}";
$result = mysqli_query($mysqli $sql);
echo "";
編號姓名
while ($row = mysqli_fetch_assoc($result)) {
{$row['id']}{$row['name']}
echo "";
}
echo "";
// 查詢總的頁數(shù)
$total_sql = "select count(*) from page";
$total_result = mysqli_fetch_array(mysqli_query($mysqli$total_sql));
$total = ceil($total_result[0]/$pageSize);
mysqli_close($mysqli);
/**
* 3.輸出分頁
*/
$showPage =5;// 顯示頁數(shù)
$banner_page = "
";
$pageOffSet = ($showPage-1)/2;//偏移量
if ($page>1) {
$banner_page.= "首頁";
$banner_page.= "上一頁";
}
// 初始化數(shù)據(jù)
$start = 1;
$end = $total;
// 數(shù)字前的省略(該部分內(nèi)容順序不可修改!!!)
if ($total>$showPage) {
if($page>$pageOffSet+1){
$banner_page .= "...";
} if($page>$pageOffSet) {
$start = $page-$pageOffSet;
$end = $total>$page+$pageOffSet?$page+$pageOffSet:$total;
}else{
$start = 1;
$end = $total>$showPage?$showPage:$total;
}if($page+$pageOffSet>$total){
$start = $start-($page+$pageOffSet-$end);
}
}
// 中間省略
for ($i=$start; $i <= $end; $i++) {
$banner_page.= "{$i}";
}
// 尾部的省略
if ($total>$showPage && $total>($page+$pageOffSet)) {
$banner_page.= "...";
}
if ($page<$total) {
$banner_page.= "下一頁";
$banner_page.= "尾頁";
}
// 自定義跳轉(zhuǎn)頁數(shù)
$banner_page.="共{$total}頁跳轉(zhuǎn)到頁";
echo "$banner_page";
?>