Problem Solution Discussion Generating Previous-Page and Next-Page Links

The script first checks t o see if a keyword param et er is present . I f so, it runs t he queries t hat look for a m at ch t o t he param et er value in t he states t able and displays t he result s. Then it present s t he form so t hat t he user can ent er a new search. When you t ry t he script , youll not ice t hat t he value of t he keyword field carries over from one invocat ion t o t he next . That s due t o CGI .pm s behavior of init ializing form fields wit h values from t he script environm ent . I f you dont like t his behavior, t o defeat it and m ake t he field com e up blank each t im e, supply an em pt y value explicit ly and an override param et er in t he textfield call: print textfield -name = keyword, -value = , -override = 1, -size = 20; Or else clear t he param et ers value in t he environm ent before generat ing t he field: param -name = keyword, -value = ; print textfield -name = keyword, -size = 20;

18.11 Generating Previous-Page and Next-Page Links

18.11.1 Problem

A query m at ches so m any records t hat displaying t hem all in a single web page produces an unwieldy result .

18.11.2 Solution

Split t he query out put across several pages and include links t hat allow t he user t o navigat e am ong pages.

18.11.3 Discussion

I f a query m at ches a large num ber of records, showing t hem all in a single web page can result in a display t hat s difficult t o navigat e. For such cases, it can be m ore convenient for t he user if you split t he result am ong m ult iple pages. Such a paged display avoids overwhelm ing t he user wit h t oo m uch inform at ion, but is m ore difficult t o im plem ent t han a single-page display. A paged display t ypically is used in a search cont ext t o present records t hat m at ch t he search param et ers supplied by t he user. To sim plify t hings, t he exam ples in t his sect ion dont have any search int erface. I nst ead, t hey im plem ent a paged display t hat present s 10 rows at a t im e from t he result of a fixed query: 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