Iterator Implementation Task
Create a Locations class that implements PHP’s Iterator interface. The class will iterate over all rows in a table named locations
(outlined below) and return the id and name in a key=>value form. The class must be used as it is in the Usage section below.
Assumptions
• A database connection has already been set up for you. Just use mysql_query for your queries.
• Data already exists in the table.
Table Structure
±--------------------±------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±--------------------±------------±-----±----±--------±---------------+
| location_id | int(11) | NO | PRI | NULL | auto_increment |
| location_name | varchar(32) | YES | | NULL | | ±--------------------±------------±-----±----±--------±---------------+
Usage
echo ‘
- ’; foreach (new Locations as $id => $name) {
- %s ’, $id, $name); echo ‘
printf(’
}