Database Reference
In-Depth Information
foreach my $ltr ( sort ( keys ( %ltr )))
{
# encode list of state names for this letter, generate unordered list
my $ul_str = ul ( li ([ map { escapeHTML ( $_ ) } @ { $ltr { $ltr }} ]));
push ( @items , dt ( $ltr ), dd ( $ul_str ));
}
print dl ( @items );
For another application of nested lists, see Recipe 19.5 .
19.3. Displaying Query Results as Tables
Problem
You want to display a query result as an HTML table.
Solution
Use each row of the result as a table row. To present an initial row of column labels,
supply your own or use the query metadata to obtain the column names.
Discussion
HTML tables are useful for presenting highly structured output. They're popular for
displaying the results of queries that consist of rows and columns due to the natural
conceptual correspondence between HTML tables and database tables or query results.
In addition, you can obtain column headers for the table by accessing the query metadata
(see Recipe 10.2 ). An HTML table has this basic structure:
• The table begins and ends with <table> and </table> tags and encloses a set of
rows.
• Each row begins and ends with <tr> and </tr> tags and encloses a set of cells.
• Tags for header cells are <th> and </th> . Tags for data cells are <td> and </td> .
(Typically, browsers display header cells using boldface or other emphasis.)
• Tags may include attributes. For example, to put a border around each cell, add a
border="1" attribute to the <table> tag. To right-justify a table cell, add an
align="right" attribute to the <td> tag.
Suppose that you want to display the contents of your CD collection:
mysql> SELECT year, artist, title FROM cd ORDER BY artist, year;
+------+-----------------+-------------------+
| year | artist | title |
+------+-----------------+-------------------+
| 2002 | Aradhna | Marga Darshan |
| 1999 | Charlie Peacock | Kingdom Come |
Search WWH ::




Custom Search