亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

News development paging function

Pagination is a function used by almost every website, because if there is a lot of content and no pagination is used, the page display will be very long, and it will be very troublesome to use and find, so let’s talk about this very practical function.

The general idea of ??paging:

Paging is to divide all the information in the database into a fixed number of segments, so two data are needed here:

Displayed on each page Number of information items $limitPage

Get the current number of pages $Page


Because the computer language defaults to obtaining records from 0

If Set the number displayed per page to 5, $limitPage = 5,

Then the first page $page = 1 will display three records 0, 1, 2, 3, 4

The second page $page = 2 will display three records 5, 6, 7, 8, 9, 10

The third page $page = 3 will display three records 11, 12, 13, 14, 15

And so on. . . . . . . . .

Set a parameter $limitFrom from which piece of data to start reading

You can get it from the above rules:

$limitFrom = ($page - 1) * $ limitPage;

will start to obtain data from the 0th, 5th, and 11th items respectively.


The next step is to connect to the database and count the total number of news items:

<?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語(yǔ)句
$retCount = mysqli_fetch_array($retQuery);   //獲取數(shù)量
$count = $retCount[0]?$retCount[0]:0;   //判斷獲取的新聞數(shù)量
$countNews = $count;
?>

Next, we give a variable $countPage to represent the total number of news items displayed How many pages are there

Maybe we will encounter this situation. If there are 100 pieces of information, we are given that 11 pieces of information are displayed on each page. How to deal with the remaining 1 piece of information?

Then we have to use % to determine the remainder:

<?php
$countPage = $countNews%$limitPage;   //求余數(shù)獲取分頁(yè)數(shù)量能否被除盡
if(($countPage) > 0) {  //獲取的頁(yè)數(shù)有余
  $countPage = ceil($countNews/$limitPage);    
// ceil() 函數(shù)向上舍入為最接近的整數(shù),除不盡則取整數(shù)+1頁(yè), 100個(gè)新聞每個(gè)頁(yè)面顯示11個(gè),成9個(gè)頁(yè)面,剩余1個(gè)單獨(dú)成1個(gè)頁(yè)面,這樣總共有10個(gè)頁(yè)面
} else {
  $countPage = $countNews/$limitPage;  //如果是10個(gè)新聞每個(gè)頁(yè)面顯示2個(gè),成5個(gè)頁(yè)面
}
?>

Other parameters: $prev for the previous page, $next for the next page;

In the paging function There are often clicks on "Previous Page" and "Next Page" to jump

Let me first talk about the idea of ????implementing the PHP code function:

Previous page$prev is the current page$ page -1 jumps to the previous page step by step. When the current page $page is the first page, jumping forward will be page 0. This will obviously cause bugs.

We need it here Give it a setting. When the current page $page is the first page, click "Previous Page" to set it to be displayed as the first page and no longer jump forward.

$prev = ($page - 1 <= 0 )?1:$page-1;

The next page $next is the current page $page -1 step Jump to the next page in one step. The problem is that when $page is the last page, click "Next Page" to jump back.

is similar to the previous page. We set it to display the last page. Page.

$next = ($page + 1 > $countPage)?$countPage:$page+1;

//When the current page number is greater than the total number of pages, Display the current page.

<!DOCTYPE html>
<html>
<head>  
<meta charset=utf8">  
<title>分表頁(yè)</title>
</head>
<body>
   <div>
         <a href="?page=<?php echo $prev;?>">|上一頁(yè)</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;?>">|下一頁(yè)</a>
   </div>
</body>
</html>


Continuing Learning
||
<?php $link = mysqli_connect('localhost','usermane','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("連接失敗:".mysqli_connect_error()); } $page = isset($_GET['page'])?$_GET['page']:1;//獲取當(dāng)前分頁(yè)數(shù) $limitNews = 5; //每頁(yè)顯示新聞數(shù)量 $countNews = ""; //總共有多少條新聞 $countPage = ""; //一共有多少頁(yè)數(shù) $limitFrom = ($page - 1) * $limitPage;//從第幾條數(shù)據(jù)開(kāi)始讀記錄 //每頁(yè)顯示5個(gè) //page = l limit 0 //page = 2 limit 5 //page = 3 limit 11 $sql = "select * from new"; $sqlCount = "select count(*) from new"; $retQuery = mysqli_query($link, $sqlCount); //查詢數(shù)量sql語(yǔ)句 $retCount = mysqli_fetch_array($retQuery); //獲取數(shù)量 $count = $retCount[0]?$retCount[0]:0; //判斷獲取的新聞數(shù)量 $countNews = $count; $countPage = $countNews%$limitPage; //求余數(shù)獲取分頁(yè)數(shù)量能否被除盡 if(($countPage) > 0) { //獲取的頁(yè)數(shù)有余 $countPage = ceil($countNews/$limitPage); // ceil() 函數(shù)向上舍入為最接近的整數(shù),除不盡則取整數(shù)+1頁(yè), 100個(gè)新聞每個(gè)頁(yè)面顯示11個(gè),成9個(gè)頁(yè)面,剩余1個(gè)成1個(gè)頁(yè)面 } else { $countPage = $countNews/$limitPage; } $prev = ($page - 1 <= 0 )?1:$page-1; //上一頁(yè) $next = ($page + 1 > $countPage)?$countPage:$page+1; //下一頁(yè) $result = mysqli_query($link, $sql); ?> <!DOCTYPE html> <html> <head> <meta charset=utf8"> <title>分頁(yè)</title> </head> <body> <div> <a href="?page=<?php echo $prev;?>">|上一頁(yè)</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;?>">|下一頁(yè)</a> </div> </body> </html>
submitReset Code