2nd set of eyes please - pager logic???
Posted: Sun Nov 04, 2007 10:38 pm
I'm reaching the end of my day and before I goto bed I want finish this freaking pager code I've been putting off for over a month...sleep will come to me and it will be all your doing. 
The implementation is simple. I'm basically generating a fixed number of page indicies, something like Google does. For this demonstration I use 5 page indicies.
I want to implement a balanced index pager, so for example consider these situations:
1) If there are five pages in total and the user is on page 2 the output will be:
[1][2][3][4][5]
2) If there are ten pages in total and the user is on page 5 the output will be:
[3][4][5][6][7]
From the above two outputs I hope I made it clear what I mean by balanced implementation? I don't want the active page to always be the first page index - except when just starting and there are no previous pages. Basically continuous shifting of the median active page until a balance can be reached on either side. The logic for shifting the active page on either the start or finish is where I think my code is faulted.
There is also a bug which is causing the last page to not show as an index until the second page has been reached.
Here is my current implementation:
Any help appreciated as I really could use some sleep tonight 
The implementation is simple. I'm basically generating a fixed number of page indicies, something like Google does. For this demonstration I use 5 page indicies.
I want to implement a balanced index pager, so for example consider these situations:
1) If there are five pages in total and the user is on page 2 the output will be:
[1][2][3][4][5]
2) If there are ten pages in total and the user is on page 5 the output will be:
[3][4][5][6][7]
From the above two outputs I hope I made it clear what I mean by balanced implementation? I don't want the active page to always be the first page index - except when just starting and there are no previous pages. Basically continuous shifting of the median active page until a balance can be reached on either side. The logic for shifting the active page on either the start or finish is where I think my code is faulted.
There is also a bug which is causing the last page to not show as an index until the second page has been reached.
Here is my current implementation:
Code: Select all
$pages = 5; // Show upto 5 page indicies at once - balanced
$index = $this->getActivePage();
$total = $this->getTotalPages();
//
// Calculate number of indicies to show on left and right hand side of active index - keep balanced
$start = $index - floor($pages / 2);
$finish = $index + floor($pages / 2);
$start = ($start < 0 ? 0 : $start);
$finish = ($finish > $total ? $total : $finish);
for($i=$start; $i<$finish; $i++){
if($i == $index){
echo '<li style="display: inline">'.($i+1).'</li>';
}
else{
echo '<li style="display: inline"><a href="?cmd'.$this->getCommands().'&idx='.$i.'">'.($i+1).'</a></li>';
}
}