I'm building a basic pagination bar to separate posts into pages. I have 3 numeric values:
$posts_per_page
$total_pages
$clicked_page_number
So for example, I have 31
posts. And my $posts_per_page
value is 15
, which means that $total_pages
becomes 3
.
My question is, how do I calculate the lowest and highest post number of the selected page ? In either javascript or PHP.
To explain this, using the values above, if user clicks on page 2
, then the lowest post number is 16
and the highest post number is 30
.
To get the highest, just multiply the page number by the posts per page. The min check is to catch the final page max (for example, if the last page only has 3 in it).
var highest = Math.min(totalPosts, clickedPageNumber * postsPerPage)
To get the lowest, it must be the one after the multiple of the previous pages posts per page (as every valid page must have the same min).
var lowest = (clickedPageNumber - 1) * postsPerPage + 1
It is, as someone else mentioned, basic maths, and has nothing to do with programming. However, here goes:
$min_post_number = ($clicked_page_number - 1) * $posts_per_page + 1;
$max_post_number = $clicked_page_number * $posts_per_page;
$max_post_number = $max_post_number > $total_posts ? $total_posts : $max_post_number;
function selectedPageBoundaries($clicked_page_number){
global $posts_per_page;
$rtn = array();
$rtn['lower'] = (int_val($clicked_page_number/$posts_per_page)*$posts_per_page);
$rtn['upper'] = $rtn['lower'] + $posts_per_page;
return $rtn;
}
something like this?
$lowest = 1 + $posts_per_page * ($clicked_page_number - 1);
$highest = $posts_per_page * $clicked_page_number;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.