Displaying Query Results as Lists

Problem

A query result contains a set of items that should be displayed as a structured list.

Solution

Write the list items within the proper HTML tags for the desired type of list.

Discussion

More structured than paragraphs and less structured than tables, lists provide a useful way to display a set of individual items. HTML provides several styles of lists, such as ordered lists, unordered lists, and definition lists. You may also wish to nest lists, which requires list-within-list formatting.

Lists generally consist of an opening and closing tag that surround a set of items, each of which is delimited by its own tags. List items correspond naturally to rows returned from a query, so generating an HTML list structure from within a program is a matter of encoding your query result, surrounding each row with the proper item tags, and adding the opening and closing list tags. Two approaches to list generation are common. If you want to print the tags as you process the result set, do this:

  1. Print the list opening tag.

  2. Fetch and print each result set row as a list item, including the item tags.

  3. Print the list closing tag.

Alternatively, you can process the list in memory:

  1. Store the list items in an array.

  2. Pass the array to a list generation function that adds the appropriate tags, then print the result.

The examples that follow demonstrate both approaches.

Ordered Lists

An ordered list consists of items that have a particular sequence. Browsers typically display ...

Get MySQL Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.