I am using PDO to query a MySQL database and return an array of bikes based on their model type. The multi-dimensional array that is returned contains various attributes (part number, color, size, etc.) and is indexed by incrementing numeric keys. Something like this:
[0] => Array
(
[ItemId] => KL-5000-Y
[SeatType] => Leather
[Speed] => 5
[Model] => Killer
[Color] => Yellow
)
[1] => Array
(
[ItemId] => KL-5000-B
[SeatType] => Leather
[Speed] => 5
[Model] => Killer
[Color] => Black
)
This array is assigned to the variable $results
I then have a class named Bike that is intended to map the various attributes to protected variables that I can access elsewhere in my application with public getter methods. Some of these methods contain additional logic, but the primary goal here is to add a layer of abstraction between the database structure and the representation of the attributes elsewhere in the application.
class Bike
{
private $ItemId;
private $SeatType;
private $Model;
private $Color;
public function __construct($results)
{
if(is_array($results)) {
$this->ItemId = $result[x]['ItemId'];
$this->SeatType = $result[x]['SeatType'];
$this->Model = $result[x]['Model'];
$this->Color = $result[x]['Color'];
}
}
public function getItemId()
{
return $this->ItemId;
}
public function getSeatType()
{
return $this->SeatType;
}
//etc.
The issue I am running into is:
1.) Figuring out how to properly traverse the array in my Bike class (see "[x]" above)
2.) Then figuring out how to properly instantiate the objects in my html template
The goal is to have a table that lists all of the attributes for a particular model, indexed by Item Id:
<table>
<thead>
<th>ITEM ID</th>
<th>SEAT TYPE</th>
<th>MODEL</th>
<th>COLOR</th>
</thead>
<tbody>
<?php $bike = new Bike($results); ?>
<tr>
<td><?php echo $bike->getItemId();?></td>
<td><?php echo $bike->getSeatType();?></td>
<td><?php echo $bike->getModel(); ?></td>
<td><?php echo $bike->getColor(); ?></td>
</tr>
</table>
I can get the above to echo out one object, but not multiple. Apologize in advance. I am relatively new to programming and I assume this has a relatively simple solution but I have not been able to figure it out or find it elsewhere on SO.
Thanks for any help in advance!