Put the work for this lab into a new folder
public_html\cs1109\lab12.
Use whatever built-in functions you wish.
In Part 5 of lab6, you wrote a program to add up how much change you had in your pockets. Here, you are going to rewrite this program, making it much shorter.
Take copies of my version of the form, cash.html, and its stylesheet,
cash.css, and store them in your lab12 folder:
cash.html,
cash.css.
Rewrite cash.php to get a much shorter version by using $_GET,
a second associative array and foreach.
I emphasize: there is nothing rude about this exercise. Thank you.
In China and elsewhere, ring sizes are reported on a numeric scale, 1—27. In the Chinese system, there's no simple mathematical relationship between the circumference of your finger and your ring size. Therefore, given the circumference of your finger, the easiest way to work out ring size in China is to look it up:
| Ring size (China) | Circumference of finger (mm) |
|---|---|
| 1 | 39.1 |
| 2 | 41.7 |
| 3 | 42.9 |
| 4 | 44.2 |
| 5 | 44.8 |
| 6 | 46.1 |
| 7 | 46.8 |
| 8 | 48 |
| 9 | 49.3 |
| 10 | 50.6 |
| 11 | 51.9 |
| 12 | 52.5 |
| 13 | 53.1 |
| 14 | 54.4 |
| 15 | 55.7 |
| 16 | 57 |
| 17 | 58.3 |
| 18 | 59.5 |
| 19 | 60.8 |
| 20 | 62.1 |
| 21 | 62.7 |
| 22 | 63.4 |
| 23 | 64.6 |
| 24 | 65.9 |
| 25 | 67.2 |
| 26 | 68.5 |
| 27 | 69.7 |
In other words, if the circumference of your finger is less than or equal to 39.1mm, then your ring size in China is 1; if the circumference is greater than 39.1 but less than or equal to 41.7, then your ring size is 2; and so on.
Take a copy of ringme.html; do not edit
it. It allows a user to enter the circumference of his/her finger in mm. You must write
ringme.php, which outputs his/her ring size. If the user enters a number that is
0 or less, or is more than 69.7, echo an error message. (You may find it useful to copy-and-paste
the table into your program and edit it, which will
save you some typing.)
This one doesn't involve associative arrays. It's just a short exercise to prepare you for Part 4.
We know how to create a form with radio buttons. Let's see how to create a form with checkboxes. The difference between a group of radio buttons and a group of checkboxes is that only one of the radio buttons can be selected at a time, whereas zero, one or more of the checkboxes can be selected.
The HTML for a checkbox is done with an input element with
type="checkbox". Just like a group of related radio buttons, a group of
related checkboxes are given the same name.
As an example, take a copy of languages.html;
do not edit
it. Bring it up in your browser. Look closely at the source to see how it is done.
In particular, notice the square brackets that are part
of the name.
Did you do what I just asked? Did you bring up the form and look at its source? Did you notice the square brackets? Do not proceed until you have done this.
Now write languages.php. Instead of this...
$languages = $_GET['languages'];
...one statement in your PHP might be:
if ( isset( $_GET['languages'] )
{
$languages = $_GET['languages'];
}
else // the user selected none
{
$languages = array(); // hence, we create an empty array
}
The important thing to realize is that, because of the square brackets mentioned
above, the variable $languages will be an indexed array of
the user's choices. E.g. if the user checked the HTML and CSS boxes, the
$languages array
will contain the strings HTML and CSS. (If the user selects
none of the boxes, no data will be sent from the browser, and this is
what the if above tests for.)
If you are struggling to make sense of this, then ask us to explain.
The remainder of your program should use a foreach loop to tell
the user which languages s/he selected. So the output might be, for example,
You like: HTML CSS
or
You like: HTML CSS PHP
or
You like:
etc., depending on what was selected by the user.
Take a copy of modules.html; do not edit it.
With this form, the user can choose his/her modules for second year. (I have excluded the
Software Entrepreneurship stream and Joint Honours with Chinese — just to save on typing.)
You must write modules.php, which outputs only Yes or No.
It outputs Yes if the user has made a legal set of choices; otherwise it outputs No.
Deadline for Lab 12: 1pm, Tuesday 24th January.
If you have named your files and folders correctly, your work will be collected automatically at that time by my software.
Remember that challenge exercises are always optional. They do not form part of your year's work and they are not worth any marks. They are designed for those students who finish the main exercise quickly and easily, and wish to explore further.
As far as I'm aware, none of you managed to find the 'smart' solution to last week's challenge. Therefore, last week's challenge 'rolls over' to this week. But I will now give two hints:
If you don't understand these hints (they're quite hard to explain in words), call me over and I'll draw you some pictures.
Put your new versions of huffman_challenge.html and huffman_challenege.php
lab12 folder, and I will take a look at them.