Creating a Multiple-Page Navigation Index

my nav_index; foreach my cnum sort { a = b } keys verses { nav_index .= nbsp; if nav_index; nav_index .= a {-href = cnum}, Chapter cnum; } Now display list of verses for each chapter. Precede each section with a label that shows the chapter number and a copy of the navigation index. foreach my cnum sort { a = b } keys verses { add an a name anchor for this section of the state display page .= p a {-name = cnum}, font {-size = +2}, Chapter cnum . br . nav_index; page .= join , {verses{cnum}}; add array of verses for chapter } dbh-disconnect ; page .= end_html ; print page; exit 0;

17.6.5 Creating a Multiple-Page Navigation Index

This exam ple shows a Perl script , est her2.pl, t hat is capable of generat ing any of several pages, all based on t he verses in t he book of Est her st ored in t he kjv t able. The init ial page displays a list of t he chapt ers in t he book, along wit h inst ruct ions t o select a chapt er. Each chapt er in t he list is a hyperlink t hat reinvokes t he script t o display t he list of verses in t he corresponding chapt er. Because t he script is responsible for generat ing m ult iple pages, it m ust be able t o det erm ine which page t o display each t im e it runs. To m ake t hat possible, t he script exam ines it s own URL for a chapter param et er t hat indicat es t he num ber of t he chapt er t o display. I f no chapter param et er is present , or it s value is not an int eger, t he script will display it s init ial page. The URL t o request t he init ial page looks like t his: ht t p: apache.snake.net cgi-bin est her2.pl The links t o individual chapt er pages have t he following form , where cnum is a chapt er num ber: ht t p: apache.snake.net cgi-bin est her2.pl?chapt er= cnum est her2.pl uses t he CGI .pm param funct ion t o obt ain t he chapter par am et er value like so: my cnum = param chapter; if defined cnum || cnum ~ \d+ { No artist ID was present or it was malformed } else { A valid artist ID was present } I f no chapter param et er is present in t he URL, cnum w ill be undef . Ot her w ise, cnum is set t o t he param et er value, which we check t o m ake sure t hat it s an int eger. That covers t he case where a garbage value m ay have been specified by som eone t rying t o crash t he script . Here is t he ent ire est her2.pl script : usrbinperl -w esther2.pl - display book of Esther over multiple pages, one page per chapter, with navigation index use strict; use lib qwusrlocalapachelibperl; use CGI qw:standard escape escapeHTML; use Cookbook; my title = The Book of Esther; my page = header . start_html -title = title, -bgcolor = white; my left_panel, right_panel; my dbh = Cookbook::connect ; my cnum = param chapter; if defined cnum || cnum ~ \d+ { Missing or malformed chapter; display main page with a left panel that lists all chapters as hyperlinks and a right panel that provides instructions. left_panel = get_chapter_list dbh, 0; right_panel = p strong The Book of Esther . p Select a chapter from the list at left.; } else { Chapter number was given; display a left panel that lists chapters chapters as hyperlinks except for current chapter as bold text and a right panel that lists the current chapters verses. left_panel = get_chapter_list dbh, cnum; right_panel = p strong The Book of Esther . get_verses dbh, cnum; } dbh-disconnect ; Arrange the page as a one-row, three-cell table middle cell is a spacer page .= table Tr td {-valign = top, -width = 15}, left_panel, td {-valign = top, -width = 5}, nbsp;, td {-valign = top, -width = 75}, right_panel ; page .= end_html ; print page; exit 0; ---------------------------------------------------------------------- Construct navigation index as a list of links to the pages for each chapter in the the book of Esther. Labels are of the form Chapter n; the chapter numbers are incorporated into the links as chapter=num parameters dbh is the database handle, cnum is the number of the chapter for which information is currently being displayed. The label in the chapter list corresponding to this number is displayed as static text; the others are displayed as hyperlinks to the other chapter pages. Pass 0 to make all entries hyperlinks no valid chapter has a number 0. No encoding is done because the chapter numbers are digits and dont need it. sub get_chapter_list { my dbh, cnum = _; my nav_index; my ref = dbh-selectcol_arrayref SELECT DISTINCT cnum FROM kjv WHERE bname = Esther ORDER BY cnum ; foreach my cur_cnum {ref} { my link = url . ?chapter=cur_cnum; my label = Chapter cur_cnum; nav_index .= br if nav_index; separate entries by br use static bold text if entry is for current artist, use a hyperlink otherwise nav_index .= cur_cnum == cnum ? strong label : a {-href = link}, label; } return nav_index; } Get the list of verses for a given chapter. If there are none, the chapter number was invalid, but handle that case sensibly. sub get_verses { my dbh, cnum = _; my ref = dbh-selectall_arrayref SELECT vnum, vtext FROM kjv WHERE bname = Esther AND cnum = ?, undef, cnum; my verses = ; foreach my row_ref {ref} { verses .= p escapeHTML row_ref-[0]. row_ref-[1]; } return verses eq no verses? ? p No verses in chapter cnum were found. : p Chapter cnum: . verses; }

17.6.6 See Also