GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Data::Page(3) User Contributed Perl Documentation Data::Page(3)

Data::Page - help when paging through sets of results

  use Data::Page;

  my $page = Data::Page->new();
  $page->total_entries($total_entries);
  $page->entries_per_page($entries_per_page);
  $page->current_page($current_page);

  print "         First page: ", $page->first_page, "\n";
  print "          Last page: ", $page->last_page, "\n";
  print "First entry on page: ", $page->first, "\n";
  print " Last entry on page: ", $page->last, "\n";

When searching through large amounts of data, it is often the case that a result set is returned that is larger than we want to display on one page. This results in wanting to page through various pages of data. The maths behind this is unfortunately fiddly, hence this module.

The main concept is that you pass in the number of total entries, the number of entries per page, and the current page number. You can then call methods to find out how many pages of information there are, and what number the first and last entries on the current page really are.

For example, say we wished to page through the integers from 1 to 100 with 20 entries per page. The first page would consist of 1-20, the second page from 21-40, the third page from 41-60, the fourth page from 61-80 and the fifth page from 81-100. This module would help you work this out.

This is the constructor, which takes no arguments.

  my $page = Data::Page->new();

There is also an old, deprecated constructor, which currently takes two mandatory arguments, the total number of entries and the number of entries per page. It also optionally takes the current page number:

  my $page = Data::Page->new($total_entries, $entries_per_page, $current_page);

This method get or sets the total number of entries:

  print "Entries:", $page->total_entries, "\n";

This method gets or sets the total number of entries per page (which defaults to 10):

  print "Per page:", $page->entries_per_page, "\n";

This method gets or sets the current page number (which defaults to 1):

  print "Page: ", $page->current_page, "\n";

This methods returns the number of entries on the current page:

  print "There are ", $page->entries_on_this_page, " entries displayed\n";

This method returns the first page. This is put in for reasons of symmetry with last_page, as it always returns 1:

  print "Pages range from: ", $page->first_page, "\n";

This method returns the total number of pages of information:

  print "Pages range to: ", $page->last_page, "\n";

This method returns the number of the first entry on the current page:

  print "Showing entries from: ", $page->first, "\n";

This method returns the number of the last entry on the current page:

  print "Showing entries to: ", $page->last, "\n";

This method returns the previous page number, if one exists. Otherwise it returns undefined:

  if ($page->previous_page) {
    print "Previous page number: ", $page->previous_page, "\n";
  }

This method returns the next page number, if one exists. Otherwise it returns undefined:

  if ($page->next_page) {
    print "Next page number: ", $page->next_page, "\n";
  }

This method takes in a listref, and returns only the values which are on the current page:

  @visible_holidays = $page->splice(\@holidays);

This method is useful paging through data in a database using SQL LIMIT clauses. It is simply $page->first - 1:

  $sth = $dbh->prepare(
    q{SELECT * FROM table ORDER BY rec_date LIMIT ?, ?}
  );
  $sth->execute($page->skipped, $page->entries_per_page);

This method changes the number of entries per page and the current page number such that the first item on the current page will be present on the new page.

 $page->total_entries(50);
 $page->entries_per_page(20);
 $page->current_page(3);
 print $page->first; # 41
 $page->change_entries_per_page(30);
 print $page->current_page; # 2 - the page that item 41 will show in

It has been said before that this code is "too simple" for CPAN, but I must disagree. I have seen people write this kind of code over and over again and they always get it wrong. Perhaps now they will spend more time getting the rest of their code right...

Related modules which may be of interest: Data::Pageset, Data::Page::Tied, Data::SpreadPagination.

Based on code originally by Leo Lapworth, with many changes added by by Leon Brocard <acme@astray.com>.

James Laver (ELPENGUIN)

Copyright (C) 2000-9, Leon Brocard

This module is free software; you can redistribute it or modify it under the same terms as Perl itself.
2019-05-10 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.