php pagination display 5pages range - php

I use pagination using this tutorial.
Everything is ok, but I want to change display of page numbers.
The tutorial code generates pages link as below.
For page 1 : [1] 2 3 4 > >>
For page 6 : << < 3 4 5 [6] 7 8 9 > >> ( 3 pages range before and after).
What I want to change is showing only 5pages.
For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: 1 2 3 4 [5] > >>
For page 6: << < [6] 7 8 9 10 > >>
For page 10: << < 6 7 8 9 [10] > >>
I think this part needs to be changed. I tried to search other articles but I couldn't find any. what is good logic to change? Thanks.
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
Added:
Last page also in 5 pages format
For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: 1 2 3 4 [5] > >>
For page 6: << < [6] 7 8 9 10 > >>
For page 10: << < 6 7 8 9 [10] > >>
For last page 12: << < 8 9 10 11[12]
I am trying to put 5 pages numbers as group in array, to use with in_array. No success yet.
Added:
Somehow, I make it working... But current page is always in center which is not I wanted.. :(
// range of num links to show
//$range = 3;
$range = 2;
....
// Added from here...
if (($currentpage - $range) <= 1){
$start_x = 1;
$end_x = 5;
}
else if ($currentpage >= ($totalpages - $range)){
$start_x = $totalpages - 4;
$end_x = $totalpages;
}
else {
$start_x = $currentpage - $range;
$end_x = $currentpage + $range;
}
// Until here
// loop to show links to range of pages around current page
// for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
for ($x = $start_x; $x < ($end_x + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
Result:
For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: << < 3 4 [5] 6 7 > >>
For page 6: << < 4 5 [6] 7 8 > >>
For page 10: << < 8 9 [10] 11 12 > >>
For last page 12: << < 8 9 10 11[12]
I still want like..
For page 1: [1] 2 3 4 5 > >>
For page 3: 1 2 [3] 4 5 > >>
For page 5: 1 2 3 4 [5] > >>
For page 6: << < [6] 7 8 9 10 > >>
For page 10: << < 6 7 8 9 [10] > >>
For last page 12: << < 8 9 10 11[12]

// loop to show links to range of pages around current page
for ($x = 1; $x <= ($totalpages); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
//limit for each 5 pages
if ($x % 5 == 0) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
}
} // end if
} // end for

Related

Get previous one item and next two item of current item in PHP. with max limit and current index only using for loop

i need to get previous value and next two values of a current_selected_val with max value stating in the starting only. Currently I am using this and not getting actual results. Can someone please help me in this
<?php
$pager_max = 8;
$current = 3;
for($i = 1; $i <= $pager_max; $i++) {
if ($i > ($current - $pager_max ) + 6 && $i < $current + 3) {
echo $i . '<br>';
}
}
?>
Here are the results which I wanted
If I select $current as
1 - 1 2 3 4
2 - 1 2 3 4
3 - 2 3 4 5
4 - 3 4 5 6
5 - 4 5 6 7
6 - 5 6 7 8
7 - 5 6 7 8
8 - 5 6 7 8
If I change $pager_max to any other value, then behaviour should be same. I need to use only formulas but not any functions here. Thanks in advance
function getValues(int $pager_max, int $current) {
if ($current === 1) {
return range(1, 4);
} elseif ($current + 2 >= $pager_max) {
return range($pager_max - 3, $pager_max);
} else {
return range($current - 1, $current + 2);
}
}

Pagination, for loop

I'm trying to make a pagination where I can limit how many numbers it will show in the navigation so it will be something like this
< 1 ... [3] 4 5 ... 9 >
The script I have now is working fine until I'm at the last couple of pages. Lets say I set max numbers allowed in the navigation to 4 and number of pages to 9. Then it will work to page 7, but from page 7 it will start shrink in numbers and not put them in front of the current page number. It will behave like this
< 1 ... [7] 8 9
< 1 ... [8] 9
When I'm at the last page the navigation will only show
< 1 ... 9
What i would like it to look like is like this
< 1 ... 6 [7] 8 9 >
< 1 ... 6 7 [8] 9 >
< 1 ... 6 7 8 [9]
I have tried to make this work for some hours now but, are kind of stuck. Any suggestions?
// Method to output the pagination
function output_pagination($max_per_page, $table) {
$page_number = $this->valid_page_number($this->get_page_number(), $table); // Get current page number
$num_pages = $this->count_table_rows($table) / $max_per_page; // Set the number of pages
$max_nav_pages = 4; // Max numbers of pages in the page navigation
$start = $page_number-1; // Value to start the loop on
// Set start to 1 if it equals 0
if($start === 0) {
$start = 1;
}
// Keep loop going until $i no longer is greater or equal to number of pages
for($i = $start; $i <= $num_pages; $i++) {
// Output prev arrow + '...' + 1 if its the start of the pagination and page number not equals 1 ($i = 1 and page number is > $i)
if(($i === $start) && ($page_number > 1)) {
// Check if number of pages are greater then max allowed in navigation and if page number not equals 2
if(($num_pages > $max_nav_pages) && ($page_number !== 2)) {
echo "<< ";
echo "< ";
echo "1 ";
echo " ... ";
$i = $page_number - 1;
} else {
echo "<< ";
echo "< ";
echo $i;
}
}
// Output current page in bold text if $i equals or are greater then $start and page number equals $i
else if (($i >= $start) && ($page_number === $i)) {
echo " <b>[".$i."]</b> ";
}
// Output "...", last page number and next page arrow. if we have 10 pages or more. Then break the loop. We are done.
else if($i === $start + $max_nav_pages) {
// Check if page number equals number of pages - max allowed nav pages + 1
if($page_number === $num_pages-$max_nav_pages+1) {
echo $i;
echo " >";
echo " >>";
break;
} else {
echo " ... ";
echo $num_pages;
echo " >";
echo " >>";
break;
}
}
// Output next page number if $i is greater then $start and $i + 1 is equal or greater then number of total pages
else if(($i > $start) && ($i + 1 <= $num_pages)) {
echo " ".$i." ";
}
else { // Output last page number and next page arrow. We are done.
echo $i;
echo " >";
echo " >>";
}
}
}
Let's say you are in page 8, then the loop always starts from $page_number - 1 which will be 7, which means 6 will never be displayed. You need to start your loop from $page_number - $max_nav_pages and determine which numbers should be displayed that way.

Logic behind pagination like google

What is the logic behind google's pagination behaviour?
My paginator goes something like this:
[1] 2 3 ... 184 >
< 1 [2] 3 4 ... 184 >
< 1 2 [3] 4 5 ... 184 >
< 1 2 3 [4] 5 6 ... 184 >
< 1 ... 3 4 [5] 6 7 ... 184 >
< 1 ... 4 5 [6] 7 8 ... 184 >
< 1 ... 5 6 [7] 8 9 ... 184 >
< 1 ... 6 7 [8] 9 10 ... 184 >
Here is a live version of the above example: http://www.dev.thomaskile.me/?page=test-zone&module=Paginator.
I know why this is happening; I've set the amount of page numbers to be shown on each side of current page to two (2).
I would rather have the range of numbers to be equal like this:
[1] 2 3 4 5 6 7 8 ... 184 >
< 1 [2] 3 4 5 6 7 ... 184 >
< 1 2 [3] 4 5 6 7 ... 184 >
< 1 2 3 [4] 5 6 7 ... 184 >
< 1 ... 3 4 [5] 6 7 ... 184 >
< 1 ... 4 5 [6] 7 8 ... 184 >
< 1 ... 5 6 [7] 8 9 ... 184 >
< 1 ... 6 7 [8] 9 10 ... 184 >
It's at the beginning and the end I need to make some changes, but can't figure out how to make it an easy operation...
I would like to make it flexible as well. Meaning I would like to be able to change the number of wanted pages on each side, and have the script expand and calculate it all...
Here is my code so far:
/**
* page controller buttons
* #param str $this->querySting href="URL string"
* #param str $this->pageIdentifier $_GET['this-name']
* #param int $this->numPages Total amount of pages
* #param int $this->midRange Number of pages to show on each side of current page
*/
public function prevPage()
{
if ($this->currentPage > 1){
$prevPage = ($this->currentPage - 1);
return 'prev';
}
}
public function nextPage()
{
if ($this->currentPage < $this->numPages) {
$nextPage = $this->currentPage + 1;
return 'next';
}
}
public function firstPage()
{
if ($this->currentPage > ($this->midRange + 1)) { // if number of pages between "currentPage" and "firstPage" exceeds $midRange with 1...
$firstPage .= '1'; // ...show "first page"-link
if ($this->currentPage > ($this->midRange + 2)) { // if number of pages between $currentPage and "first page" exceeds $midRange with more than 1
$firstPage .= '…'; // add "..." between "1st page"-link and first page in $range
}
}
return $firstPage;
}
public function lastPage()
{
if ($this->currentPage < ($this->numPages - $this->midRange)) { // if number of pages between "currentPage" and "last page" is equal to $midRange
if (($this->currentPage < ($this->numPages - $this->midRange) - 1)) { // if number of pages between $currentPage and "last page" exceeds $range with more than two
$lastPage .= '…'; // add "..." between "last page"-link and last page in $range
}
$lastPage .= ''.$this->numPages.''; // show "last page"-link
}
return $lastPage;
}
# Range of pages between (prev first ...) and (... last next)
public function listPages()
{
for ($i = ($this->currentPage - $this->midRange); $i < (($this->currentPage + $this->midRange) + 1); $i++){
if (($i > 0) && ($i <= $this->numPages)) // if page number are within page range
{
if ($i == $this->currentPage) { $listPages .= '<a class="current">'.$i.'</a>'; } // if we're on current page
else { $listPages .= ''.$i.''; } // if not current page
}
}
return $listPages;
}
This is what I do for my Pagination.
$startPage = $currentPage - 4;
$endPage = $currentPage + 4;
if ($startPage <= 0) {
$endPage -= ($startPage - 1);
$startPage = 1;
}
if ($endPage > $totalPage)
$endPage = $totalPage;
if ($startPage > 1) echo " First ... ";
for($i=$startPage; $i<=$endPage; $i++) echo " {$i} ";
if ($endPage < $totalPage) echo " ... Last ";
I believe my code is self-explained, but I will try to explain it in plain English. First of all, you need to know two things before you can generate Pagination: $totalPage and $currentPage.
Step 1: Assuming that the current page is in mid-range. $startPage and $endPage store range of page that pagination try to generate.
Step 2: If $startPage is negative, then you need to make-up for $endPage.
Step 3: If $endPage excess $totalPage, then $endPage is the last page.
Step 4: Generating Pagination into HTML. (it is up to you how you want your pagination to look. I will simply use plain text to represent my pagination)
if ($startPage > 1) echo " First ... ";
for($i=$startPage; $i<=$endPage; $i++) echo " {$i} ";
if ($endPage < $totalPage) echo " ... Last ";
Fixed flaw to my previous logic
$startPage = ($curPage < 5)? 1 : $curPage - 4;
$endPage = 8 + $startPage;
$endPage = ($totalPage < $endPage) ? $totalPage : $endPage;
$diff = $startPage - $endPage + 8;
$startPage -= ($startPage - $diff > 0) ? $diff : 0;
if ($startPage > 1) echo " First ... ";
for($i=$startPage; $i<=$endPage; $i++) echo " {$i} ";
if ($endPage < $totalPage) echo " ... Last ";
This conversation was a great start for me! But I wanted a paginator closer to the intentions of the original question, that:
1) Could be contained in a function with variables to alter the total pages, current page, and number of pages on each side of the current to show.
2) Maintains a constant width, similar to the original post:
< [1] 2 3 4 5 6 7 ... 99 >
< 1 [2] 3 4 5 6 7 ... 99 >
< 1 2 [3] 4 5 6 7 ... 99 >
< 1 2 3 [4] 5 6 7 ... 99 >
< 1 2 3 4 [5] 6 7 ... 99 >
< 1 ... 4 5 [6] 7 8 ... 99 >
< 1 ... 5 6 [7] 8 9 ... 99 >
< 1 ... 92 93 [94] 95 96 ... 99 >
< 1 ... 93 94 [95] 96 97 98 99 >
< 1 ... 93 94 95 [96] 97 98 99 >
< 1 ... 93 94 95 96 [97] 98 99 >
< 1 ... 93 94 95 96 97 [98] 99 >
< 1 ... 93 94 95 96 97 98 [99] >
3) Continues to display the number "2" rather than "..." in cases where you would have 1 ... 3
4) Same thing for the end.
So here's what I did. I am coding in a different language (coffeescript), but it should function as good sudo-code anyway:
get_pages_array = (total_page, each_side, curr_page) ->
if total_page <= (2*each_side)+5
# in this case, too few pages, so display them all
start_page = 1
end_page = total_page
else if curr_page<=each_side+3
# in this case, curr_page is too close to the beginning
start_page = 1
end_page = (2*each_side)+3
else if curr_page >= total_page - (each_side+2)
# in this case, curr_page is too close to the end
start_page = total_page - (2*each_side) - 2
end_page = total_page
else
# regular case
start_page = curr_page - each_side
end_page = curr_page + each_side
return_me = []
if start_page> 1
return_me.push "1"
if start_page>2
return_me.push "..."
for x in [start_page..end_page]
return_me.push x
if end_page<total_page-1
return_me.push "..."
if end_page<total_page
return_me.push total_page
return return_me
I am using this code for each_side = 2, so that's where I'm sure it works.
EDIT: fixed logic as per #Vextil
Here's a Python program that shows how to do this correctly:
def main():
num_pages = 13
page = 12
window = 5
start = page - window
end = page + window - 1
if start <= 0:
end = end - start + 1
start = 1
if end > num_pages:
end = num_pages
start = max(end - (window * 2) + 1, 1)
for no in range(start, end + 1):
print "{}*".format(no) if page == no else no
if __name__ == '__main__':
main()
This is pure awesome! I think I got this paginator to work the way I described.
Please, have a look and try it out here http://dev.thomaskile.me/?page=test-zone&module=Paginator and let me know...
After a lot of logical math studying I finally came to this conclusion:
In order to make this act so differently on different levels, there have to be some if, elsef-s to handle the logic on each level seperatly. I'll try to explain, but find it hard to do in a good way...
These are the levels I'm talking about:
If currentPage == firstPage :
Calculate how many pages to show after currentPage starting from 2nd page.
This calculation needed to be done based on how many page boxes there would be at the most. (midRange value is a key factor here)
[1] 2 3 4 5 6 7 8 ... 184 >
elseif currentPage is in between firstPage and midRange value maxed out.
Reduce pages in range by one to prevent moving the whole paginator to the right once prevPage is added.
Calculate pages to show before and after currentPage to keep the amount of pages equal trough the whole thing.
< 1 [2] 3 4 5 6 7 ... 184 >
< 1 2 [3] 4 5 6 7 ... 184 >
< 1 2 3 [4] 5 6 7 ... 184 >
elseif midRange value is maxed out on each side. Meaning we're in the middle somewhere.
midRange pages + the current page + midRange pages. Quite straight forward i guess...
< 1 ... 3 4 [5] 6 7 ... 184 >
...
...
...
< 1 ... 178 179 [180] 181 182 ... 184 >
elseif currentPage is in between midRange value and lastPage
Almost the same as in the beginning. Difference was to calculate a static pagenumber to start pages from, then calculate pages to show before/after current page...
(this, by the way, has been my headache this weekend)
< 1 ... 178 179 180 [181] 182 183 184 >
< 1 ... 178 179 180 181 [182] 183 184 >
< 1 ... 178 179 180 181 182 [183] 184 >
elseif currentPage == numPages (number of tatal pages).
Pretty much same as firstPage operation... calculating how many pages needed to fill the whole thing up and calculate where to start from...
What I need to do now is to make the code itself better...
< 1 ... 178 179 180 181 182 183 [184] >
The "problem" in my case was that the whole paginator should calculate everything based on the midRange value and nothing else.
For me to execute this paginator in any of my future project, all I have to do is:
$paginator = new paginator((int)); // e.g. number of total results from a db request
I might in most cases need to add a personal querystring to make sure the a href is working:
$paginator->set_queryString('my querystring');
And that's pretty much all. I've set up a couple of optional functions like this:
$paginator->set_resultsPerPage((int));
$paginator->set_midRange((int));
$paginator->set_pageIdentifier('querystring-pageNumber-identifier-name-for-get'); // whatever I needed
Finally i display the paginator page controller like this:
$paginator->pageController('full'); // full, med, min for different styles.
If non of these are good enough, i could just call each button like this:
$paginator->prevPage();
$paginator->firstPage();
$paginator->listPages();
$paginator->lastPage();
$paginator->nextPage();
$paginator->pageJumper();
$paginator->perPageSelector();
I assume your pagination have this structure:
number_of_active_page + separate(...) + page(184) + next_page(>)
You can set number_of_active_page become 8
( include prev_page(<) + pages ( ... and page number )
[1] 2 3 4 5 6 7 8 ... 184 >
[number_of_active_page(set to 8)] + separate + page + next_page
< 1 ... 3 4 [5] 6 7 ... 184 >
import math
size = 3
len = 13
for page in range(1,10):
if(( size*(page-1) ) >len):
startPoint = (size*(page-1)) - (size*(page- math.ceil(len/size)))
else:
startPoint = ( size*(page-1) )
if((startPoint +size)>len):
endPoint = len
else:
endPoint = (startPoint +size -1)
print("Page = "+str(page))
print("start = " +str(startPoint))
print("end = " +str(endPoint))
print()
Hear is a simple example of pagination display:
$paginationDisplay = ""; // Initialize the pagination output variable
// This code runs only if the last page variable is not equal to 1,
// if it is only 1 page we require no paginated links to display
if ($lastPage != "1"){
// This shows the user what page they are on, and the total number of pages
$paginationDisplay .= 'Page <strong>' . $pn .
'</strong> of ' . $lastPage. 'last';
// If we are not on page 1 we can place the Back button
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= ' <a href="' .
$_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
}
// Lay in the clickable numbers display here between the Back and Next links
$paginationDisplay .= '<span>' . $centerPages . '</span>';
// If we are not on the very last page we can place the Next button
if ($pn != $lastPage) {
$nextPage = $pn + 1;
$paginationDisplay .= ' <a href="' .
$_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
}
}
This is the pagination logic I have
$pLinks = 5; // Links per page
$pMids = 3;
$pTot = 10; // Total page
$pSel = 1 // Selected page
if (($pSel <= $pMids) || ($pTot <= $pLinks)) {
$sPage = 1;
$ePage = ($pTot <= $pLinks) ? $pTot : $pLinks;
} else {
$etPage = $pSel + ($pMids - 1);
$ePage = ($etPage <= $pTot) ? $etPage : $pTot;
$sPage = $ePage - ($pLinks - 1);
}
if ($pSel > $sPage) {
$sL = 'First';
$sN = '«';
} else {
$sL = 'First';
$sN = '«';
}
if ($pSel < $ePage) {
$eL = 'End';
$eN = '»';
} else {
$eL = 'End';
$eN = '»';
}
$pOptions = '';
$pOptions .= '<span class="iPage">'.$pSel.'/'.$pTot.'</span>';
$pOptions .= '<span class="renderFL">'.$sL.'</span>';
$pOptions .= '<span class="renderPN">'.$sN.'</span>';
for ($i = $sPage; $i <= $ePage; $i++) {
if($i != $pSel) {
$pOptions .= '<span>'.$i.'</span>';
} else {
$pOptions .= '<span class="selected">'.$i.'</span>';
}
}
$pOptions .= '<span class="renderPN">'.$eN.'</span>';
$pOptions .= '<span class="renderFL">'.$eL.'</span>';
The result would be look like this:
1 -> [1] 2 3 4 5
2 -> 1 [2] 3 4 5
3 -> 1 2 [3] 4 5
..
5 -> 3 4 [5] 6 7
6 -> 4 5 [6] 7 8
..
8 -> 6 7 [8] 9 10
9 -> 6 7 8 [9] 10
10 -> 6 7 8 9 [10]

Add Ellipsis in PHP Pagination

.
//Prev
.
for($number = 1; $number <= $num_pages; $number++)
{
if($page == $number)
{
$navigator .= "<b>[$number]</b> ";
}
else
{
$navigator .= "<a href='?c=".$_SESSION['cID']".&rows=".$per_page."&page=$number'>$number</a> ";
}
}
.
//Next
.
This is the snippet that prints number of pages.
Sample output:
Previous 1 2 3 4 [5] 6 7 8 9 10 Next
5 is the current page.
Problem: page numbers are shown in sequence with no restrictions. If i have 100 pages, all numbers show up.
Question: I need my paging numbers appear as the following...
Assume we only have 7 ($num_pages) pages:
Previous 1 2 [3] 4 5 6 7 Next
Assume we have 90 pages:
[1] 2 3 4 5 6 7 ... 90 Next
Assume user clicked the 7th page:
Previous 1 ... 5 6 [7] 8 9 10 11 ... 90 Next
Assume user clicked 11th page:
Previous 1 ... 9 10 [11] 12 13 14 15 ... 90 Next
Assume user clicked 15th page:
Previous 1 ... 13 14 [15] 16 17 18 19 ... 90 Next
Assume user clicked 90th page:
Previous 1 ... 84 85 86 87 88 89 [90]
Any help will be appreciated.
$radius = 3;
for($i = 1; $i <= $total; $i++){
if(($i >= 1 && $i <= $radius) || ($i > $current - $radius && $i < $current + $radius) || ($i <= $total && $i > $total - $radius)){
if($i == $current) echo "<b>".$i."</b>";
}
elseif($i == $current - $radius || $i == $current + $radius) {
echo "... ";
}
}
This should be more than enough to get you started at least
$count = 7; // number to show
// start at half threshold down from the current location.
$number = $current - round($count/2);
if( $number > 1 ) echo '...';
else $ // increase to have number start at 1.
for( $number; $number < $number + $count; $number++)
{
// your for loop as normal
}
if( $number < $total ) echo '...';
An elegant solution for this kind of thing is to use "logarithmic page navigation". See my answer to this question (PHP code included):
How to do page navigation for many, many pages? Logarithmic page navigation

stuck at displaying a PHP paginator

I'm trying to create a paginator. I have created a paginator class with properties you can see below. But now I'm trying to display the paginator, but I'm completely stuck on different loops (perhaps I should just go to bed :P) and stuff.
The important properties are:
windows_size // the amount of pages visible
num_pages // the total number of pages
current_page // the currently selected page
I have the following object:
o: (Paginator)
->item_count : 295
page_size : 50
window_size : 5
num_pages : 10
current_page: 1
first_index : 50
And I want to display:
[1] 2 3 4 5 ...
Or I have:
o: (Paginator)
->item_count : 295
page_size : 50
window_size : 5
num_pages : 10
current_page: 5
first_index : 50
... 3 4 [5] 6 7...
Or I have:
o: (Paginator)
->item_count : 295
page_size : 50
window_size : 5
num_pages : 10
current_page: 9
first_index : 50
... 6 7 8 [9] 10
How would I get this to be displayed?
END RESULTS (many thanks to: Wh1T3h4Ck5!)
http://pastebin.com/2KzL1Tn6
Generally, it's very simple piece of code.
You can use something like this.
function myPaginator($total_items, $items_per_page, $visible_pages_pad, $active_page) {
$res = '';
if ($items_per_page > 0 && $active_page > 0 && $visible_pages_pad) {
$max_page = floor($total_items/$items_per_page);
if ($total_items % $items_per_page > 0) $max_page++;
if ($active_page <= $max_page) {
$visible_pages = $visible_pages_pad * 2 + 1;
if ($max_page <= $visible_pages) {
$minp = 1;
$maxp = $max_page;
}
else {
$minp = $active_page - $visible_pages_pad;
if ($minp < 1) $minp = 1;
$maxp = $minp + $visible_pages - 1;
if ($maxp > $max_page) {
$maxp = $max_page;
$minp = $maxp - $visible_pages + 1;
}
}
for ($i = $minp; $i <= $maxp; $i++) {
$page = $i == $active_page ? "[$i]" : $i; // create links here
$res .= "$page ";
}
$res = trim($res);
if ($minp > 1) $res = '... ' . $res; // left dots
if ($maxp < $max_page) $res .= ' ...'; // right dots
}
}
return $res == '' ? false : $res;
}
Parameters:
$total_items - total number of items in list
$items_per_page - how menu items you want on page
$visible_pages_pad - left/right padding (this value * 2 + 1 = number of paginator items)
$active_page - selected page in paginator
Result:
Function myPaginator() returns
false if something is wrong, otherwise returns string.
Limitations:
all parameters should be integers
$items_per_page > 0
$visible_pages_pad > 0
1 <= $active_page <= {TOTAL NUMBER OF PAGES}
Usage:
$total_items = 250; // total items
$items_per_page = 20; // 250/20 = 13 pages
$visible_pages_pad = 2; // 5 pages visible in paginator (2*2+1)
$active_page = 1;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: [1] 2 3 4 5 ...
$active_page = 2;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: 1 [2] 3 4 5 ...
$active_page = 3;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: 1 2 [3] 4 5 ...
$active_page = 6;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 4 5 [6] 7 8 ...
$active_page = 11;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 9 10 [11] 12 13
$active_page = 12;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 9 10 11 [12] 13
$active_page = 13;
echo myPaginator($total_items,$items_per_page,$visible_pages_pad,$active_page);
// output: ... 9 10 11 12 [13]
Note: $visible_pages_pad is number of pages in paginator left/right of the active-page number.
Hope this helps...

Categories