Paged Displays with Previous-Page and Next-Page Links

SELECT name, abbrev, statehood, pop FROM states ORDER BY name; MySQL m akes it easy t o select j ust a port ion of a result set : add a LIMIT clause t hat indicat es which records you want . The t wo- argum ent form of LIMIT t akes values indicat ing how m any records t o skip at t he beginning of t he result set , and how m any t o select . The query t o select a sect ion of t he states t able t hus becom es: SELECT name, abbrev, statehood, pop FROM states ORDER BY name LIMIT skip , select ; One issue, t hen, is t o det erm ine t he proper values of skip and select for any given page. Anot her is t o generat e t he links t hat point t o ot her pages or t he query result . One st yle of paged display present s only previous page and next page links. To do t his, you need t o know whet her any records precede or follow t hose youre displaying in t he current page. Anot her paging st yle displays a link for each available page. This allows t he user t o j um p direct ly t o any page, not j ust t he previous or next page. To present t his kind of navigat ion, you have t o know how t he t ot al num ber of records in t he result set and t he num ber of records per page, so t hat you can det erm ine how m any pages t here are.

18.11.4 Paged Displays with Previous-Page and Next-Page Links

The follow ing script , st at e_pager1.pl, present s records from t he states t able in a paged display t hat includes navigat ion links only t o t he previous and next pages. For a given page, we can det erm ine which links are needed as follows: • A previous page link is needed if t here are records in t he result set preceding t hose shown in t he current page. I f t he current page st art s at record one, t here are no such records. • A next page link is needed if t here are records in t he result set following t hose shown in t he current page. You can det erm ine t his by issuing a SELECT COUNT query t o see how m any records t he query m at ches in t ot al. Anot her m et hod is t o select one m ore record t han you need. For exam ple, if youre displaying 10 records at a t im e, t ry t o select 11 records. I f you get 11, t here is a next page. I f you get 10 or less, t here isnt . st at e_pager1.pl uses t he lat t er approach. To det erm ine it s current posit ion in t he result set and how m any records t o display, st at e_pager1.pl looks for start and per_page input param et ers. When you first invoke t he script , t hese param et ers wont be present , so t heyre init ialized t o 1 and 10, respect ively. Thereaft er, t he script generat es previous page and next page links t o it self t hat include t he proper param et er values in t he URLs for select ing t he previous or next sect ions of t he result set . usrbinperl -w state_pager1.pl - paged display of states, with prev-pagenext-page links use strict; use lib qwusrlocalapachelibperl; use CGI qw:standard escape escapeHTML; use Cookbook; my title = Paged US State List; my page = header . start_html -title = title, -bgcolor = white . h3 title; my dbh = Cookbook::connect ; Collect parameters that determine where we are in the display. Default to beginning of result set, 10 recordspage if parameters are missingmalformed. my start = param start; start = 1 if defined start || start ~ \d+ || start 1; my per_page = param per_page; per_page = 10 if defined per_page || per_page ~ \d+ || per_page 1;; If start 1, then well need a live previous page link. To determine whether or not there is a next page, try to select one more record than we need. If we get that many, display only the first per_page records, but add a live next page link. Select the records in the current page of the result set, and attempt to get an extra record. If we get the extra one, we wont display it, but its presence tells us there is a next page. my query = sprintf SELECT name, abbrev, statehood, pop FROM states ORDER BY name LIMIT d,d, start - 1, number of records to skip per_page + 1; number of records to select my tbl_ref = dbh-selectall_arrayref query; dbh-disconnect ; Display results as HTML table my rows; push rows, Tr th [Name, Abbrevation, Statehood, Population]; for my i = 0; i per_page i {tbl_ref}; i++ { get data values in row i my cells = {tbl_ref-[i]}; get data values in row i map values to HTML-encoded values, or to nbsp; if nullempty cells = map { defined _ _ ne ? escapeHTML _ : nbsp; } cells; add cells to table push rows, Tr td \cells; } page .= table {-border = 1}, rows . br ; If were not at the beginning of the query result, present a live link to the previous page. Otherwise present static text. if start 1 live link { my url = sprintf s?start=d;per_page=d, url , start - per_page, per_page; page .= [ . a {-href = url}, previous page . ] ; } else static text { page .= [previous page]; } If we got the extra record, present a live link to the next page. Otherwise present static text. if {tbl_ref} per_page live link { my url = sprintf s?start=d;per_page=d, url , start + per_page, per_page; page .= [ . a {-href = url}, next page . ]; } else static text { page .= [next page]; } page .= end_html ; print page; exit 0;

18.11.5 Paged Displays with Links to Each Page