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:

  1. Using what you've learned so far, clean all of the incoming fields.
  2. Perform the following checks to insure you have all the required fields:
    1. Check to be sure that the 'temperature' is set and numeric. If it is not, set an error.
    2. Check to be sure that the 'from' is set and not empty. If it is not, set an error.
  3. If there are no errors, check for the following four cases and deal with them accordingly:
    1. For Fahrenheit to Celsius, calculate using the formula C = (F - 32) * 5/9
    2. For Celsius to Fahrenheit, calculate using the formula F = C * 9/5 + 32
    3. 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.)
  4. 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

  1. Don't forget to properly prepare strings for printing.
  2. 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:

$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:

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

  1. The array must be defined using the array() function, and its key-value pairs must be assigned using the key/value operator (=>).
  2. Do not use the individual access operator ([ ]) and the assignment operator ( = ) to assign the pairs.
  3. 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:

  1. Print the players' array in the original order:
    1. Loop through all the players elements using the foreach construct;
    2. Display in a table the following pieces of information:
      1. player's last name and first name;
      2. player's nationality;
      3. player's position.
  2. Sort the players array on the key.
  3. 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

  1. 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.
  2. 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:

Hints

  1. 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.
  2. Don't forget to properly prepare all strings for HTML output. (Hint: Use htmlentities() when printing string variables.)
  3. You may whatever loop structure your prefer to access the $winners array (for, while, foreach, etc.).

Quick Link

Grading (for instructors)