1

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!

2
  • 1
    Would you like your class Bike, to return information on multiple bikes, or just one Bike? Commented May 22, 2014 at 22:49
  • Multiple bikes. There's 200 unique bikes, we group them by model and want to display each model group in a table highlighting their various attributes. Commented May 23, 2014 at 3:02

2 Answers 2

2

You're thinking about it in the wrong way. You're trying to turn an array of attributes for multiple bikes into a bike, when you should be turning an array multiple bike attributes into an array of bikes.

To get this idea into code, use this for your class:

class Bike 
{
    private $ItemId;
    private $SeatType;
    private $Model;
    private $Color;

    public function __construct($result) 
    {
        if(is_array($result)) {
            // since $results is now only a single array, there is no need for [x]
            $this->ItemId            = $result['ItemId'];
            $this->SeatType          = $result['SeatType'];
            $this->Model             = $result['Model'];
            $this->Color             = $result['Color'];
        }
    }

    public function getItemId()
    {
        return $this->ItemId;
    }

    public function getSeatType()
    {
        return $this->SeatType;
    }
    .....

So, first we need to put our bikes into an array:

<?php
    $bikes = array();
    foreach ($results as $key => $attributes) {
        $bikes[] = new Bike($attributes);
    }
?>

Then print out each bike into your table:

<table>
    <thead>
        <th>ITEM ID</th>
        <th>SEAT TYPE</th>
        <th>MODEL</th>
        <th>COLOR</th>
    </thead>
    <tbody>

    <?php foreach ($bikes as $key => $bike): ?>
    <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>
    <?php endforeach ?>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is exactly what I was looking for. Slightly un-related follow-up. The section of code that puts our bikes into an array, is there a place that bit of code is best stored if using an MVC type of approach or framework. I'd prefer not to put it into my html template, but it doesn't belong to the class either...
You'd put that in your controller and pass it to your view.
1

PDO can return more types than just arrays. Actually, one way it can return data is as an instantiated object. Not even as just a stdObject either.

Check this out:

class Bike
{
    public $id;
    public $seatType;
    public $model;
    public $color;


    public function getColor(){return $this->color}

}


$stmt = $pdo->prepare('SELECT * FROM bike');
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_CLASS, 'Bike');

This will return an array of instantiated bikes.

1 Comment

Whoa, that's cool! Maybe it's time to re-read the PDO documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.