Paged Displays with Links to Each Page

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

The next script , st at e_pager2.pl, is m uch like st at e_pager1.pl, but present s a paged display t hat includes navigat ion links t o each page of t he query result . To do t his, it s necessary t o know how m any row t here are in all. st at e_pager2.pl det erm ines t his by running a SELECT COUNT query. Because it t hen knows t he t ot al row count , it s unnecessary t o select an ext ra row when fet ching t he sect ion of t he result t o be displayed. Om it t ing t he part s of st at e_pager2.pl t hat are t he sam e as st at e_pager1.pl, t he m iddle part t hat ret rieves records and generat es links is im plem ent ed as follows: Determine total number of records my total_recs = dbh-selectrow_array SELECT COUNT FROM states; Select the records in the current page of the result set 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; 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 {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 ; Generate links to all pages of the result set. All links are live, except the one to the current page, which is displayed as static text. Link label format is [m to n] where m and n are the numbers of the first and last records displayed on the page. for my first = 1; first = total_recs; first += per_page { my last = first + per_page - 1; last = total_recs if last total_recs; my label = first to last; my link; if first = start live link { my url = sprintf s?start=d;per_page=d, url , first, per_page; link = a {-href = url}, label; } else static text { link = label; } page .= [link] ; }

18.12 Generating Click to Sort Table Headings