|There's more than one way to do things|
Programmatically building named anchors to warp to sectionsby hacker (Priest)
|on Aug 15, 2007 at 01:05 UTC||Need Help??|
hacker has asked for the
wisdom of the Perl Monks concerning the following question:
I've thrown together a web front end for a database of terms that some users have asked me to compile for them. The database is very small, and consists of id,term,description.
The front end simply builds a list of the terms with named anchors that link to the full summary of the term lower in the page. It basically looks like this:
So clicking on Pacifier with the named anchor, will bring you to the section summary for Pacifier. So far, so good...
What has been suggested, since the list of terms can get very long, is to create some named anchors which will "warp" to specific sections in the page. For example, <a href="#Pa">Pa</a> would jump you to the part of the page that contains terms that start with "Pa". <a href="#Pe">Pe</a> would jump lower to the section of terms beginning with "Pe", etc.
I can cut down the amount of vertical scrolling through a long list, by making them "Pa", "Pe", "Pi", "Po", "Pu", for each letter of the alphabet ($letter . [aeiou];)
The problem I'm having, is that my query is in a $sth->fetch() loop that prints each term out of the database. In this construct, how do I "slice" each term coming out, and determine when the first two letters change from "Pa" to "Pe", and set a named anchor directly before the change?
I can't quite figure out the logic to make that happen, programmatically.