Paginating - page links algorithms

Matts on 2003-07-08T13:21:55

I'm displaying a series of pages of results from a search query. Data::Page rocks for doing the nitty gritty details. But what Data::Page doesn't do for you is give you a simple algorithm for links to multiple pages where you don't want to overflow the page with links.

I'm probably not being clear, so an example will help.

You might have 100 pages of results. I don't want to show links for 1..100 at the bottom of my page. I want something more like what google does - shows you a limit around the current page you're on. So if you're on page 33, you get links for pages 30..40 (with 33 highlighted) and the rest you can see by moving through the list.

Has anyone created a simple algorithm for something like that? Am I missing a key module from CPAN?


How about...

dlc on 2003-07-08T13:37:16

You should be able to do something like:

  my $pages_to_display = 10; # or whatever
  my $current = 1; # The current page (3rd arg
                    # to Data::Page::new)
  my ($start, $end);
  $start = $current - ($pages_to_display / 2);
  $end = $current + ($pages_to_display / 2);

  for ($start .. $end) {
      # make start and end links

(adding checks for boundary conditions is left as an exercise ;)

You end up with $pages_to_display + 1 links (counting the current page) in the row of pages links.

Data::Pageset

davorg on 2003-07-08T13:38:55

Isn't that what Data::Pageset does?

Maybe you want something flavored like this?

wickline on 2003-07-09T03:54:55

Well, I tried to post it here, but slashcode got in my way. Instead, look over here.

-matt