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(*)是计算数据总的条数 $retQuery = mysqli_query($link, $sqlCount); //查询数量sql语句 $retCount = mysqli_fetch_array($retQuery); //获取数量 $count = $retCount[0]?$retCount[0]:0; //判断获取的新闻数量 $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; //求余数获取分页数量能否被除尽 if(($countPage) > 0) { //获取的页数有余 $countPage = ceil($countNews/$limitPage); // ceil() 函数向上舍入为最接近的整数,除不尽则取整数+1页, 100个新闻每个页面显示11个,成9个页面,剩余1个单独成1个页面,这样总共有10个页面 } else { $countPage = $countNews/$limitPage; //如果是10个新闻每个页面显示2个,成5个页面 } ?>
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>分表页</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>