Practice 4

Main | Static vs. Dynamic Websites | Using PHP | A Simple PHP Program | Variables and Operators | Practice 1 | Solution 1 | Loops | Practice 2 | Solution 2 | Functions | Practice 3 | Solution 3 | Conditionals | Practice 4 | Solution 4

This exercise will test your knowledge of conditionals and give you more experience in using loops. It will also expose you to using PHP for dynamic HTML table creation. In this practice we will examine a standard HTML table, and then use PHP to generate the same table dynamically by using loops and conditionals.

In this exercise you must create the following table with PHP.

1 2 3 4 5 6 7 8
no no no no no yes yes yes

The above table was generated dynamically using PHP loops. To do this we start by simply outputting the basic HTML code to construct the web page and begin a table. Then, we use PHP to start a for loop to count from 1 to 8. Within this loop, we display the HTML code to first start a table cell, and then display the variable we are counting with ($i). We then finally display the HTML necessary to close the table cell. One advantage of making tables this way, is that it is easy to generate one hundred columns instead of just eight, by simply changing one value in our loop.

In this exercise you will be provided with the loops that generate the tables as discussed above. It will be your job to find and program the condition that will make the second row say "yes" or "no" as above.

If you examine the above table you should be able to find a simple relation between the numbers (1, 2, ..., 8) in the first row, and whether the second row says "no" or "yes". Once you find this relation you should be able to express it in the form of a conditional and echo the appropriate data at the appropriate time.

To complete this exercise you must:

  1. Create a file in "public_html/php" called "dynamicTables.php".
  2. Make "dynamicTables.php" viewable from the web.
  3. Fill in "dynamicTables.php" with this incomplete code.
  4. Complete the PHP section of the incomplete code so that it satisfies the following:
    • Find the condition needed to make the program echo "yes" and "no" at the appropriate times.


jfulton [at] member.fsf.org
22 Aug 2013