Examples of linking pages in html pagination. Bootstrap - Pagination (navigation block for pagination)

Pagination, including page navigation, is a fairly simple thing, but beginners often have problems creating it, and most importantly, understanding how it works...

One of the tasks of pagination is the simultaneous output of not all elements, but a limited number of them, say, no more than $pp elements, and which particular group of elements is output depends on the input parameter $pn - the number of this group. This number is, in fact, the page number. Here you need a query like this:

SELECT * FROM `table` LIMIT (($pn-1)*$pp),($pp)

The value of $pn is decreased by 1 so that, for example, for the first page of the list with a value of $pp equal to 10, elements from 0 to 9 are selected, and not from 10 to 19. Naturally, if pages are numbered from zero, the value of $ is decreased by 1 pn is not needed in the request.

To solve the next problem, namely building page navigation, you first need to know the total number of pages in the list. To do this, you can first query the total number of elements. Or, directly in the query, calculate the total number of pages based on the total number of elements:

SELECT FLOOR((COUNT(*)+($pp-1))/($pp)) FROM `table`

To get the total number of pages $pc, the fairly well-known formula $pc=(count+per_page-1) div per_page is used here, but adjusted to use the FLOOR function instead of integer division (div). You can also use the DIV operator, which has been supported in MySQL for quite some time.

If you only need to display one element per page, the above queries can be simplified.

Once the total number of pages $pc is obtained, you can immediately display links to all pages of the list using a loop with a counter in the range from 1 to $pc , however, usually a more sophisticated navigation is used, in which links to not all pages are displayed simultaneously, but only to pages with numbers in a limited range depending on the current page number. For example, here are the formulas for obtaining the range-limiting values ​​of $first and $last for the so-called bank navigation:

$first=$pn-1-($pn-2)%$range; $last=$first+$range$pc) error(404); elseif ($result=mysqli_query($link,"SELECT * FROM `table` LIMIT ".(($pn-1)*$pp).",".$pp)) ( $range=6; $first=$ pn-1-($pn-2)%$range; $last=$first+$range>