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.
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:
strlen
function.)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.
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 Cuphooray!.) 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...
for
statement with proper management of counter variables. (
Hint: ascending implies increment, descending implies decrement.)for
loop.htmlentities()
when printing string variables.)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...
foreach
loop to iterate through all the players.
(Hint: We need the jersey number and the player information).[Jersey Number]. [Last name, First name]
;
htmlentities()
when printing string variables.)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:
foreach
loop to iterate through all the players.
(Hint: You will need the jersey number and the player information.) For
each entry:
list()
and explode()
to do this.)[Jersey Number]. [First Name] [Last Name]
;
implode()
function, display the starting line-up last names in a comma separated list format.Again, the $italy2006 array has been provided, so you may assume that its data is clean.