0290280 - MacKay Wilford
Weekly assignment for lesson 4
Review Exercise
Required
Description
Please use the review.php
file as base for your
exercise. The objectives for this exercise are to review form processing and numeric manipulation concepts we covered in earlier weeks.
Scenario: Temperature Conversions
Since this is an international class, we're going to build a temperature converter. To keep things simple, we'll only convert between two scales, Fahrenheit and Celsius. Your file contains a very short form that gathers two pieces of information: the temperature you wish to convert and the scale you wish to convert from. Once your visitor has chosen the "from" scale, the script simply assumes that you wish to convert to the other scale.
You have the following tasks:
- Using what you've learned so far, clean all of the incoming fields.
- Perform the following checks to insure you have all the required fields:
- Check to be sure that the 'temperature' is set and numeric. If it is not, set an error.
- Check to be sure that the 'from' is set and not empty. If it is not, set an error.
- If there are no errors, check for the following four cases and deal with them accordingly:
- For Fahrenheit to Celsius, calculate using the formula C = (F - 32) * 5/9
- For Celsius to Fahrenheit, calculate using the formula F = C * 9/5 + 32
- Finally, if neither is the case, issue an "invalid selection" error. (Note that the only way this can occur is if someone is attempting to crack your form.)
- Print errors or success: For the success case, please indicate the temperature and scale you are converting from, the scale your are converting to and the calculated value.
Hints and Restrictions
- Don't forget to properly prepare strings for printing.
- By definition, true numbers cannot contain HTML entity characters. You may prepare numeric input by casting. Calculations are safe and may be printed as-is or formatted using
printf
.
Quick Link
Grading (for instructors)
Exercise no. 1
Required
Description
Please use the exercise1.php
file as base for your
exercise. The main objective for this exercise is to practice intializing and filling multi-dimensional arrays using the array()
construct.
As with last week's exercise, the soccer team is used as an example of a collection only. There's no need to know anything about the game.
The table below provides some information about four players who have won the Europen Footballer of the Year award:
Name |
Nationality |
Position |
Nedved,
Pavel |
Czech |
Midfielder |
Shevchenko,
Andriy |
Ukrainian |
Attacker |
Ronaldinho,
Ronaldo de Assis Moreira |
Brazilian |
Attacker |
Cannavaro,
Fabio |
Italian |
Defender |
Kaká,
Ricardo Izecson dos Santos Leite |
Brazilian |
Midfielder |
You have been given two initialized arrays, one containing the most common positions in soccer and one containing country and nationality information.
The $positions array is an associative array. For each entry in the array, the value is the name of a position and the key is its uppercase initial letter:
$positions = array (
'G' => 'Goalkeeper',
'D' => 'Defender',
'M' => 'Midfielder',
'A' => 'Attacker'
);
The $countries array is a multi-dimensional associative array. It is structured as follows:
- key: 2-digit ISO code (e.g.
IT
for Italy);
- value: associative array with the following fields:
- nationality (e.g. 'Italian')
- country (e.g. 'Italy')
$countries = array (
'BR' => array (
'nationality' => 'Brazilian',
'country' => 'Brazil',
),
'CZ' => array (
'nationality' => 'Czech',
'country' => 'Czech Republic',
),
'IT' => array (
'nationality' => 'Italian',
'country' => 'Italy',
),
'UK' => array (
'nationality' => 'Ukrainian',
'country' => 'Ukraine',
)
);
Task one: construct an associative multi-dimensional array for the players
Using the array()
construct, initialize and fill a multidimensional array to hold the players' information. It should reflect the following structure:
- key: player's identifier (in this case the lowercase
last name is good enough, e.g. 'nedved');
- value: associative array with the following fields:
- first name (e.g.: 'Pavel')
- last name (e.g.: 'Nedved')
- country: 2-digit ISO code as in the
$countries
array (e.g. CZ
for Czech Republic)
- position: single-character key for the position in the
$positions
array (e.g. M
for midfielder).
Task two: dump the contents of all three arrays
Once the players' array has been initialized, dump the contents of the three arrays using the print_r()
function.
Restrictions and Hints
- The array must be defined using the
array()
function, and its key-value pairs must be assigned using the key/value operator (=>
).
- Do not use the individual access operator (
[ ]
) and the assignment operator ( =
) to assign the pairs.
- Since all of the information has been provided as a given, you may assume that the contents are clean. There is no need to clean any of the arrays.
Quick Link
Grading (for instructors)
Exercise no. 2
Required
Description
Please use the exercise2.php
file as base for your
exercise. Insert the players array that you created in the previous exercise into it. (Copy and paste only the array definition, not the print dumps).
This exercise will focu on multi-dimensional arrays, the foreach
loop, basic key sorting operations, and accessing individual elements. As usual, refer to the online solution as
model for your development process, but be as creative as you like in terms of layout and graphics.
Here are the required tasks for this exercise:
-
Print the players' array in the original order:
-
Loop through all the players elements using the
foreach
construct;
-
Display in a table the following pieces of information:
-
player's last name and first name;
-
player's nationality;
-
player's position.
-
Sort the players array on the key.
-
Print the players' array as sorted, using the same methodology as in the previous task with just one addition: the display of the key in another column of the table.
Hints
- Since all data has been provided as a given (or you have constructed it), you may assume that the array contents are clean. There is no need to clean them before using them.
- Don't forget to properly prepare all strings for HTML output. (Hint: Use
htmlentities()
when printing string variables.)
Quick Link
Grading (for instructors)
Exercise no. 3
Optional
Description
Please use the exercise3.php
file as base for your
exercise. Insert the players array that you created in exercise 1 into it. (Just copy and paste the array definition.) The main objective for this exercise is for you to
become familiar with the array_key_exists
function.
Have a look at the online solution and observe its behaviour.
You have been given a sequentially indexed array named $winners, which is structured as follows:
$winners = array (
'platini',
'baggio',
'cannavaro',
'ronaldo',
'ronaldinho',
'zidane');
Your task is to loop through the $winners array elements, and for each element:
- Check to see whether the current winner value is a key in your players array
- If it is, print:
- the winner element value (e.g. 'platini');
- the literal string 'found';
- the first name and the last name of the player, taken from your players array using the direct access operator (
[key]
)
- If it is not, print:
- the winner element value (e.g. 'platini');
- the literal string 'not found.'
Hints
- Since all data has been provided as a given (or you have constructed it), you may assume that the array contents are clean. There is no need to clean them before using them.
- Don't forget to properly prepare all strings for HTML output. (Hint: Use
htmlentities()
when printing string variables.)
- You may whatever loop structure your prefer to access the $winners array (
for
, while
, foreach
, etc.).
Quick Link
Grading (for instructors)