Problem Solution Discussion Creating a Navigation Index from Database Content

return sprintf a href=\mailto:s\sa, addr, htmlspecialchars name; } def make_email_link name, addr: return name as static text if address is None or empty if addr is None or addr == : return cgi.escape name, 1 return a hyperlink otherwise return a href=\mailto:s\sa addr, cgi.escape name, 1 For a JSP page, you can produce t he newsstaff list ing as follow s: sql:query var=rs dataSource={conn} SELECT department, name, email FROM newsstaff ORDER BY department, name sql:query ul c:forEach var=row items={rs.rows} li c:out value={row.department} : c:set var=name value={row.name} c:set var=email value={row.email} c:choose -- null or empty value test -- c:when test={empty email} c:out value={name} c:when c:otherwise a href=mailto:c:out value={email} c:out value={name} a c:otherwise c:choose li c:forEach ul

17.6 Creating a Navigation Index from Database Content

17.6.1 Problem

A list of it em s in a web page is long. You want t o m ake it easier t o m ove around in t he page.

17.6.2 Solution

Creat e a navigat ional index cont aining links t o different sect ions of t he list .

17.6.3 Discussion

I t s easy t o display list s in w eb pages Recipe 17.3 . But if a list cont ains a lot of it em s, t he page cont aining it m ay becom e quit e long. I n such cases, it s oft en useful t o break up t he list int o sect ions and provide a navigat ion index, in t he form of hyperlinks t hat allow users t o reach sect ions of t he list quickly wit hout scrolling t he page m anually. For exam ple, if you ret rieve records from a t able and display t hem grouped int o sect ions, you can include an index t hat let s t he user j um p direct ly t o any sect ion. The sam e idea can be applied t o m ult iple- page displays as well, by providing a navigat ion index in each page so t hat users can reach any ot her page easily. This sect ion provides t wo exam ples t o illust rat e t hese t echniques, bot h of which are based on t he kjv t able t hat was int roduced in Recipe 4.12 . The exam ples im plem ent t wo kinds of display, using t he verses from t he book of Est her st ored in t he kjv t able: • A single- page display t hat list s all verses in all chapt ers of Est her. The list is broken int o 10 sect ions one per chapt er , wit h a navigat ion index t hat has links point ing t o t he beginning of each sect ion. • A m ult iple- page display consist ing of pages t hat each show t he verses from a single chapt er of Est her, and a m ain page t hat inst ruct s t he user t o choose a chapt er. Each of t hese pages also displays a list of chapt ers as hyperlinks t o t he pages t hat display t he corresponding chapt er verses. These links allow any page t o be reached easily from any ot her. 17.6.4 Creating a Single-Page Navigation Index This exam ple displays all verses in Est her in a single page, wit h verses grouped int o sect ions by chapt er. To display t he page so t hat each sect ion cont ains a navigat ion m arker, place an a name anchor elem ent before each chapt ers verses: a name=1Chapter 1a ... list of verses in chapter 1 ... a name=2Chapter 2a ... list of verses in chapter 2 ... a name=3Chapter 3a ... list of verses in chapter 3 ... ... This generat es a list t hat includes a set of m arkers nam ed 1 , 2 , 3 , and so fort h. To const ruct t he navigat ion index, build a set of hyperlinks, each of which point s t o one of t he name m arkers: a href=1Chapter 1a a href=2Chapter 2a a href=3Chapter 3a ... The in each href at t ribut e signifies t hat t he link point s t o a locat ion wit hin t he sam e page. For exam ple, href=3 point s t o t he anchor w it h t he name=3 at t ribut e. To im plem ent t his kind of navigat ion index, you can use a couple of approaches: • Ret rieve t he verse records int o m em ory and det erm ine from t hem which ent ries ar e needed in t he navigat ion index. Then print bot h t he index and verse list . • Figure out all t he applicable anchors in advance and const ruct t he index first . The list of chapt er num bers can be det erm ined by t his st at em ent : SELECT DISTINCT cnum FROM kjv WHERE bname = Esther ORDER BY cnum; You can use t he query result t o build t he navigat ion index, t hen fet ch t he verses for t he chapt ers lat er t o creat e t he page sect ions t hat t he index ent ries point t o. Heres a script , est her1.pl, t hat uses t he first approach. I t s an adapt at ion of one of t he nest ed- list exam ples shown in Recipe 17.3 . usrbinperl -w esther1.pl - display book of Esther in a single page, 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 . h3 title; my dbh = Cookbook::connect ; Retrieve verses from the book of Esther and associate each one with the list of verses for the chapter it belongs to. my sth = dbh-prepare SELECT cnum, vnum, vtext FROM kjv WHERE bname = Esther ORDER BY cnum, vnum; sth-execute ; my verses = ; while my cnum, vnum, vtext = sth-fetchrow_array { initialize chapters verse list to empty array if this is first verse for it, then add verse numbertext to array. verses{cnum} = [ ] unless exists verses{cnum}; push {verses{cnum}}, p escapeHTML vnum. vtext; } Determine all chapter numbers and use them to construct a navigation index. These are links of the form a href=numChapter numa, where num is a chapter number a signifies a within-page link. No URL- or HTML-encoding is done here the text that is displayed here doesnt need it. Make sure to sort chapter numbers numerically use { a = b }. Separate links by non-breaking spaces. 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