CS 371, Fall 2001
Lab 3: Tables in HTML

An HTML table displays items in neat rows and columns. They are often used to neatly lay out the elements in a form or different parts of a page. Fundamentally, tables are a simple idea. A table is created using the <table>...</table> element. Inside this element are the rows of the table. Each row is created using the <tr>...</tr> element (where "tr" stands for "table row"). Inside a <tr> element are the individual items that go in the cells of the table. Each item is created with a <td>...</td> element (where "td" stands for "table data"). You can put anything at all inside a <td> element: simple text, paragraphs, lists, images, even another table. So, the outline of the HTML code to create a table looks like this:

      <table>

         <tr>
            <td> ... </td>
            <td> ... </td>
               .
               .
         </tr>

         <tr>
            <td> ... </td>
            <td> ... </td>
               .
               .
         </tr>
         
           .
           .
           .

      </table>
      

Here, for example, is a very simple table and the HTML source code that creates it:

<table>
   <tr>
      <td>(Row 1, Column 1)</td>
      <td>(Row 1, Column 2)</td>
      <td>(Row 1, Column 3)</td>
   </tr>
   <tr>
      <td>(Row 2, Column 1)</td>
      <td>(Row 2, Column 2)</td>
      <td>(Row 2, Column 3)</td>
   </tr>
</table>
(Row 1, Column 1) (Row 1, Column 2) (Row 1, Column 3)
(Row 2, Column 1) (Row 2, Column 2) (Row 2, Column 3)

However, there are a lot of optional attributes that you can apply to the <table>, <tr>, and <td> tags. This lab looks at some of the more useful options.

You might want to look at some of my pages that use tables. Here are three:



Options for the <table> Tag

There are several useful attributes that can be used in the table tag. You can see some of the more common attributes demonstrated on my TableDemo page. Attributes include:



Options for the <tr> Tag

Options specified in a <tr> tag affect all the cells in the row. The most useful attributes are align, valign, and bgcolor, which simply set the default values for these attributes for all the <td> tags in the row. See the next section for their meaning.


Options for the <td> Tag

The options in a <td> tag affect just one cell in the table, except for width and height, which can have implied effects on other cells in the same column or row.

One final note on cells: It's generally not a good idea to have cells that don't contain any data at all. If you want an empty cell, put a non-breaking space in the <td>. This would look like <td>&nbsp;</td> in the HTML source code.


Assignment

Exercise 1: Make an improved version of your survey form (or a new form) that uses tables to lay out the elements of the form more neatly. Put a link to form on your main page for the course, and hand in a printout of the source code.

Exercise 2: In class, we looked at a function that could create an entire group of radio buttons. Write a similar function,

function makeSelect($varname, $options)

that will create a <select> menu. The first parameter gives the name of the menu, and of the associated PHP variable. The second parameter is an array that contains the various options for the menu in positions 0, 1, 2,.... Use your function either on a demo page or in the form that you create for Exercise 1. If it's on a separate page, turn in a printout of the source code and put a link to the page on your main page.


David Eck, September 14, 2001