???? ??? ??
???? ?? ?? ?????? ???? ?????. ???? ?? ???? ???? ??? ??? ??? ?? ??? ???? ?? ? ?? ???? ? ?? ??? ?? ???? ?? ?? ???? ?????. ??.
???? ???? ??:
???? ??????? ?? ??? ??? ??? ?? ????? ??? ???? ???? ? ?? ???? ?????.
? ???? ???? ?? ?? ? $limitPage
?? ??? ?? $Page ????
??? ??? ????? 0?? ???? ???? ?????
???? ?? ?? 5? ???? $limitPage = 5,
? ?? ??? $page = 1 ? 0, 1, 2 , 3 , 4 ? ?? ???? ?????
? ?? ??? $page = 2 ? 5 , 6 , 7 , 8 , 9 , 10 ? ?? ???? ?????
? ?? ??? $page = 3? 11, 12, 13, 14, 15 ? ?? ???
?? ?????. . . . . . . . .
??? ??? ??? $limitFrom ???? ??
?? ???? ??? ?? ? ????.
$limitFrom = ($page - 1) * $limitPage;
?? 0?? 5??? ?????. , 11? ???? ???? ?? ?????.
?? ??? ??????? ???? ? ?? ?? ?? ???? ????.
<?php $link = mysqli_connect('localhost','uesrname','password','news'); $sql = "select * from new"; // select * from表示獲取全部字段的值 $sqlCount = "select count(*) from new"; //count(*)是計(jì)算數(shù)據(jù)總的條數(shù) $retQuery = mysqli_query($link, $sqlCount); //查詢數(shù)量sql語句 $retCount = mysqli_fetch_array($retQuery); //獲取數(shù)量 $count = $retCount[0]?$retCount[0]:0; //判斷獲取的新聞數(shù)量 $countNews = $count; ?>
?? ?? ?? $countPage? ???? ? ???? ??? ?? ?????.
??? ??? ??? ?? ??? ??? ?????. ??? 100??? ? ???? 11?? ?????. ??? 1?? ???? ??? ??? ????.
?? ?? %? ???? ???? ???? ???.
<?php $countPage = $countNews%$limitPage; //求余數(shù)獲取分頁數(shù)量能否被除盡 if(($countPage) > 0) { //獲取的頁數(shù)有余 $countPage = ceil($countNews/$limitPage); // ceil() 函數(shù)向上舍入為最接近的整數(shù),除不盡則取整數(shù)+1頁, 100個(gè)新聞每個(gè)頁面顯示11個(gè),成9個(gè)頁面,剩余1個(gè)單獨(dú)成1個(gè)頁面,這樣總共有10個(gè)頁面 } else { $countPage = $countNews/$limitPage; //如果是10個(gè)新聞每個(gè)頁面顯示2個(gè),成5個(gè)頁面 } ?>
?? ???? ?? ??? $prev, ?? ??? $next
??? ???? "?? ???"? "?? ???"? ???? ???? ??? ????
?? ????? ?? ????????. PHP ?? ??? ?? ?????:
?? ??? $page -1? $prev? ?? ??? $page? ? ?? ???? ? ??? ???? ???? 0? ???. . ??? ??? ??? ??? ????.
???? ?? ??? $page? ? ?? ???? ?? "?? ???"? ???? ? ?? ???? ???? ? ?? ??? ???? ??? ???? ???. .
$prev = ($page - 1 <= 0 )?1:$page-1;
?? ??? $next? ?? ??? $page -1?? ???? ?? ???? ?????. ??? $page? ??? ???? ? "Next Page"? ???? ?? ???? ?????.
?? ???? ???? ??? ???? ????? ??????.
$next = ($page + 1 > $countPage)?$countPage:$page+1;
//?? ??? ??? ?? ??? ???? ? ?? ?? ???? ?????.
<!DOCTYPE html> <html> <head> <meta charset=utf8"> <title>分表頁</title> </head> <body> <div> <a href="?page=<?php echo $prev;?>">|上一頁</a> <?php for($i=1; $i<=$countPage; $i++):?> <a href="?page=<?php echo $i;?>"><?php echo $i;?></a> <?php endfor;?> <a href="?page=<?php echo $next;?>">|下一頁</a> </div> </body> </html>