# Data::Page.3pm

Langue: en

Autres versions - même langue

Version: 2004-10-28 (mandriva - 01/05/08)

Section: 3 (Bibliothèques de fonctions)

# Sommaire

## NAME

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

## SYNOPSIS

```   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";

```

## DESCRIPTION

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.

## METHODS

new

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);

```

total_entries

This method get or sets the total number of entries:

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

```

entries_per_page

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

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

```

current_page

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

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

```

entries_on_this_page

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

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

```

first_page

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";

```

last_page

This method returns the total number of pages of information:

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

```

first

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

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

```

last

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

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

```

previous_page

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";
}

```

next_page

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";
}

```

splice

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

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

```

skipped

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(\$date, \$page->skipped, \$page->entries_per_page);

```

## NOTES

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...