1Data::Page(3) User Contributed Perl Documentation Data::Page(3)
2
3
4
6 Data::Page - help when paging through sets of results
7
9 use Data::Page;
10
11 my $page = Data::Page->new();
12 $page->total_entries($total_entries);
13 $page->entries_per_page($entries_per_page);
14 $page->current_page($current_page);
15
16 print " First page: ", $page->first_page, "\n";
17 print " Last page: ", $page->last_page, "\n";
18 print "First entry on page: ", $page->first, "\n";
19 print " Last entry on page: ", $page->last, "\n";
20
22 When searching through large amounts of data, it is often the case that
23 a result set is returned that is larger than we want to display on one
24 page. This results in wanting to page through various pages of data.
25 The maths behind this is unfortunately fiddly, hence this module.
26
27 The main concept is that you pass in the number of total entries, the
28 number of entries per page, and the current page number. You can then
29 call methods to find out how many pages of information there are, and
30 what number the first and last entries on the current page really are.
31
32 For example, say we wished to page through the integers from 1 to 100
33 with 20 entries per page. The first page would consist of 1-20, the
34 second page from 21-40, the third page from 41-60, the fourth page from
35 61-80 and the fifth page from 81-100. This module would help you work
36 this out.
37
39 new
40 This is the constructor, which takes no arguments.
41
42 my $page = Data::Page->new();
43
44 There is also an old, deprecated constructor, which currently takes two
45 mandatory arguments, the total number of entries and the number of
46 entries per page. It also optionally takes the current page number:
47
48 my $page = Data::Page->new($total_entries, $entries_per_page, $current_page);
49
50 total_entries
51 This method get or sets the total number of entries:
52
53 print "Entries:", $page->total_entries, "\n";
54
55 entries_per_page
56 This method gets or sets the total number of entries per page (which
57 defaults to 10):
58
59 print "Per page:", $page->entries_per_page, "\n";
60
61 current_page
62 This method gets or sets the current page number (which defaults to 1):
63
64 print "Page: ", $page->current_page, "\n";
65
66 entries_on_this_page
67 This methods returns the number of entries on the current page:
68
69 print "There are ", $page->entries_on_this_page, " entries displayed\n";
70
71 first_page
72 This method returns the first page. This is put in for reasons of
73 symmetry with last_page, as it always returns 1:
74
75 print "Pages range from: ", $page->first_page, "\n";
76
77 last_page
78 This method returns the total number of pages of information:
79
80 print "Pages range to: ", $page->last_page, "\n";
81
82 first
83 This method returns the number of the first entry on the current page:
84
85 print "Showing entries from: ", $page->first, "\n";
86
87 last
88 This method returns the number of the last entry on the current page:
89
90 print "Showing entries to: ", $page->last, "\n";
91
92 previous_page
93 This method returns the previous page number, if one exists. Otherwise
94 it returns undefined:
95
96 if ($page->previous_page) {
97 print "Previous page number: ", $page->previous_page, "\n";
98 }
99
100 next_page
101 This method returns the next page number, if one exists. Otherwise it
102 returns undefined:
103
104 if ($page->next_page) {
105 print "Next page number: ", $page->next_page, "\n";
106 }
107
108 splice
109 This method takes in a listref, and returns only the values which are
110 on the current page:
111
112 @visible_holidays = $page->splice(\@holidays);
113
114 skipped
115 This method is useful paging through data in a database using SQL LIMIT
116 clauses. It is simply $page->first - 1:
117
118 $sth = $dbh->prepare(
119 q{SELECT * FROM table ORDER BY rec_date LIMIT ?, ?}
120 );
121 $sth->execute($page->skipped, $page->entries_per_page);
122
123 change_entries_per_page
124 This method changes the number of entries per page and the current page
125 number such that the first item on the current page will be present on
126 the new page.
127
128 $page->total_entries(50);
129 $page->entries_per_page(20);
130 $page->current_page(3);
131 print $page->first; # 41
132 $page->change_entries_per_page(30);
133 print $page->current_page; # 2 - the page that item 41 will show in
134
136 It has been said before that this code is "too simple" for CPAN, but I
137 must disagree. I have seen people write this kind of code over and over
138 again and they always get it wrong. Perhaps now they will spend more
139 time getting the rest of their code right...
140
142 Related modules which may be of interest: Data::Pageset,
143 Data::Page::Tied, Data::SpreadPagination.
144
146 Based on code originally by Leo Lapworth, with many changes added by by
147 Leon Brocard <acme@astray.com>.
148
150 James Laver (ELPENGUIN)
151
153 Copyright (C) 2000-9, Leon Brocard
154
156 This module is free software; you can redistribute it or modify it
157 under the same terms as Perl itself.
158
159
160
161perl v5.28.0 2009-12-07 Data::Page(3)