0290280 - MacKay Wilford

Weekly assignment for lesson 3

Review Exercise

Required

Description

Please use the review.php file as base for your exercise. The objectives for this exercise are to review the form processing and string manipulation concepts we covered last week and to practice some simple (and useful) data processing.

Scenario: Famous Quotes

You'd like people to submit famous quotes for inclusion in a database, but you're the pithy sort and only want short quotes that will fit into a tinytext column in your MySQL database. This data type has a maximum length of 255 characters, so you will need to (nicely) reject any quote that is longer than that.

You've decided to use a TEXTAREA tag for this. Unlike the INPUT TYPE="text" field, TEXTAREA does not have an attribute which limits the length of its input. In order to limit the input, you must code your script accordingly.

You have the following tasks:

  1. Using what you learned last week, clean the incoming $_POST['quote'] and $_POST['citation'] fields.
  2. Check to be sure that the fields are not empty.
  3. Print errors or succes: For the case where the input is too long, print both the amount of the overflow and the truncated string. (Hint: Don't forget to properly prepare the strings for HTML output.)

Note: As outlined above, this script is not as user-friendly as it could be: It doesn't check for length errors unless both variables are present, so it's possible to send the user back twice to correct errors instead of catching all at once. Ideally, we would collect all errors and report them at once; this complicates the logic, however. IF you feel comfortable with the more advanced logic, you may choose to code the more user-friend method.

Quick Link

Grading (for instructors)

Exercise no. 1

Required

Description

Please use the exercise1.php file as base for your exercise. The main objectives for this exercise are for you to become familiar with sequentially indexed arrays and the simple for loop.

If you don't know anything about football (soccer), don't worry. The sport is a marginal aspect of these exercises. A team (which is a type of collection) happens to be useful for practicing with arrays and loops.

In honor of the 2006 World Cup, we pay a small tribute to the Italian national team. (For those of you who don't know, Italy won the World Cup—hooray!.) We will use the Final's starting line-up as data for our array practice.

Your exercise1.php file contains the following sequentially indexed array, named $italy2006. The array elements are the players who formed the starting line-up for the 2006 final game:

$italy2006 = array ( 'Buffon, Gianluigi', 'Grosso, Fabio', 'Cannavaro, Fabio', 'Gattuso, Gennaro', 'Toni, Luca', 'Totti, Francesco', 'Camoranesi, Mauro', 'Zambrotta, Gianluca', 'Perrotta, Simone', 'Pirlo, Andrea', 'Materazzi, Marco' );

Each entry is made up of a string having the following format:

[LAST_NAME], [FIRST_NAME]

For instance, the first entry is Italy's goalkeeper: Buffon, Gianluigi where Buffon is the last name and Gianluigi the first name.

Take a look at the our solution and follow it as an example for your data output. As always, we are interested in PHP stuff and the logic behind the application, so feel free to be as creative as you like on the layout.

You have two simple tasks: Use for loops to display the properly prepared starting line-up...

  1. in ascending order (e.g. from Buffon to Materazzi);
  2. in descending order (e.g. from Materazzi to Buffon).

Restrictions and Hints

  1. Use only the for statement with proper management of counter variables. ( Hint: ascending implies increment, descending implies decrement.)
  2. Do not use array sorting functions, even if you already know how to use them. The point of this exercise is to practice the for loop.
  3. Since $italy2006 has been provided as a given, you may assume that its contents are clean. There is no need to clean it before using it.
  4. Don't forget to properly prepare strings for HTML output. (Hint: Use htmlentities() when printing string variables.)

Quick Link

Grading (for instructors)

Exercise no. 2

Required

Description

Please use the exercise2.php file as base for your exercise. The main objectives for this exercise are to become with associative indexed arrays and foreach loops. Use the online solution as model for your development process.

Your exercise2.php file contains the following associative array, named $italy2006. As you can see, we've turned it into an associative array for this exercise:

$italy2006 = array ( 1 => 'Buffon, Gianluigi', 3 => 'Grosso, Fabio', 5 => 'Cannavaro, Fabio', 8 => 'Gattuso, Gennaro', 9 => 'Toni, Luca', 10 => 'Totti, Francesco', 16 => 'Camoranesi, Mauro', 19 => 'Zambrotta, Gianluca', 20 => 'Perrotta, Simone', 21 => 'Pirlo, Andrea', 23 => 'Materazzi, Marco' );

Each entry is a (key => value) pair, where the key is the player's jersey number and the value is the player's name, in the same format as in the previous exercise:

[LAST_NAME], [FIRST_NAME]

This exercise's tasks are to...

  1. Use a foreach loop to iterate through all the players. (Hint: We need the jersey number and the player information).
  2. For each entry, display the player information in the following format:
    [Jersey Number]. [Last name, First name];

Hints

  1. Since $italy2006 has been provided as a given, you may assume that its contents are clean. There is no need to clean it before using it.
  2. Don't forget to properly prepare 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. The main objectives for this exercise are for you to practice with associative indexed arrays and foreach loops and to become familiar with list(), explode() and implode().

As usual refer to the online solution as a model for your development process.

We will use the same array we used in exercise 2. Actually, the first part of the exercise is very similar. However, we will also work on more advanced topics such as direct access of associative array elements, string-to-array conversions and vice versa.

In addition to the array, you have also been given the following two lines of code:

$scorer = 23; ... $last_names = array();

The variable named $scorer holds the jersey number of the player who scored Italy's goal. If you look up the number 23 in the $scorer array, you will see that it is the key for the string 'Materazzi, Marco'. Although this is an associative array, we are using an integer value as the key (look up). This is perfectly acceptable. Unlike a true numerically indexed array, the integers needn't be consecutive, though they must be unique.

The $last_names array has been initialized and is ready to be used in the exercise. During the exercise, you will fill it with the last names of the players, using the same structure and organization as the $italy2006 array. Example:

$array[$key] = $value;

This exercise's tasks are to:

  1. Use a foreach loop to iterate through all the players. (Hint: You will need the jersey number and the player information.) For each entry:
    1. Split the last name and first name parts, without using any string related function such as strtok(). (Hint: You can combine list() and explode() to do this.)
    2. Properly fill the $last_names array with the current player's information.
    3. Display the player information in the following format:
      [Jersey Number]. [First Name] [Last Name];
  2. Using the information contained in the $last_names array and the implode() function, display the starting line-up last names in a comma separated list format.
  3. Display the last name of the player who scored the first goal, combining the information contained in the $last_names array and the $scorer variable. (Hint: perform a lookup in the array using the $scorer variable as key.)
  4. Properly prepare all string data for printing.

Again, the $italy2006 array has been provided, so you may assume that its data is clean.

Quick Link

Grading (for instructors)