This lab will encourage you to explore some of the possibilities when working with array variables in PHP. For practice in a realistic application, you will do this using data from this form. Some default values have been filled in so that you won't have to type them every time you want to test things, but you can and should change these defaults when testing your page. To set up the lab, save a copy on your development platform (either your own laptop or your course account on beowulf). The form is set to post its results to register.php. You will write this document yourself.
Starting Out
Start with the skeleton of an empty html document. You will add content in the body section.
Recall that the values from the form will be stored in the superglobal array called $_POST. It will be useful to look at the format of this array, and others we will develop from it. The print_r function provides a quick (if not very friendly) way to view what is in any array.
<?php print_r($_POST); ?>
Let's begin by writing our own piece of code to display the form values. Ours will be a mixture of HTML and PHP elements, and will build a table of the form key/value pairs. The code is shown below as an example:
<table> <tr><th>Key</th><th>Value</th></tr> <?php foreach($_POST as $key => $value) { echo "<tr><td>$key</td><td>$value</td></tr>"; } ?></table>
So far, so good. But the array of key/value pairs is not really what we want. It would be much more convenient if we had an array associating an entrant's name with their belt color. Then we could create many tables that might be more interesting to the people running the tournament. Creating this associative array is a bit tricky, although it uses only the builtin PHP array handling functions. The solution is shown below. It sorts the form results by the keys, putting all the belts in the first half and all the names in the second half. This makes it simple to take just the half with the names, and the belts must be everything that was left. Finally the two pieces are combined in the array called $entrants.
<?php $formdata = $_POST; ksort($formdata); $names = array_slice($formdata,count($formdata)/2); $belts = array_diff_assoc($formdata,$names); $entrants = array_combine($names,$belts); ?>
Your Turn
Try the following exercises to practice with arrays:
- Create a new table, similar to the one showing key/value pairs. The new table will show entrants and their belt levels.
- Copy the table above, but first sort the entrants by their belt color. (Make sure you still have the correct name/belt combiantions!)
- Copy the table above, but first sort the entrants by their name. (Make sure you still have the correct name/belt combiantions!)
- Write combined HTML and PDF code to produce a list of entrants' names.
- Create a new array using array_count_values and use it to produce a table showing the number of entrants at each belt level.
- If you know how to write a conditional (an if statement), try producing a list of the names of all entrants who are white belts.