Welcome Guest, Not a member yet? Register   Sign In
slight pagination improvement
#1

[eluser]neomech[/eluser]
I really love the pagination class but found the look when you were at the first or last couple of pages to be a little odd.

I'm used to seeing the same number of numerical links visible no matter what page I'm on. So assuming 5 links if I'm on page 7 I'll see:
Code:
<< < 5,6,7,8,9 > >>
and if I'm on page 1 I'll see
Code:
1,2,3,4,5 > >>

The CI pagination class counts how many numbers to use before and after the current page you're on, so if you're at the first page or last page, you'll see fewer numbered links than you'd expect, like so:

Code:
1,2,3 > >>
A similar phenomenon occurs on the last page. The second and second-last pages (assuming 5 links) only show 4 links as well.

I added this bit of code to my own pagination class right after $start and $end are assigned their values (see lines 168 & 169 of CI 1.7.1 pagination class), and it fixes this small issue:
Code:
for($linkcounter=0 ; $linkcounter <= $this->num_links ; $linkcounter++)
{
    if ($this->cur_page == $linkcounter)
    {
        $end = min ($end + ($this->num_links - $linkcounter + 1), $num_pages);
    }
    if ($this->cur_page == ($num_pages - $linkcounter + 1))
    {
        $start = max ($start - ($this->num_links - $linkcounter +1), 1);
    }
}
Now a consistent number of numerical links are created, even if you're in the first few pages or the last few pages, and the pagination class works more like the typical "pagination" that you see on most websites.

To make it easier to understand what this is doing, here's the same thing without the foreach loop, assuming you have $num_links set to 2:
Code:
if ($this->cur_page == 1)
{
    $end=min($end+2,$num_pages);
}
if ($this->cur_page == 2)
{
    $end=min($end+1,$num_pages);
}

if ($this->cur_page == $num_pages)
{
    $start=max($start-2,1);
}
if ($this->cur_page == $num_pages-1)
{
    $start=max($start-1,1);
}
Hope someone find this useful. This is my first time trying to post something helpful (I'm new!), so please let me know if I could improve it somehow.




Theme © iAndrew 2016 - Forum software by © MyBB