'CSV to PHP class properties

I'm working on an assignment right now, and I'm trying to take the data from a CSV and make them into class objects. So each item in the CSV row needs to be a class property.

Bicycle Class

class Bicycle
{

    public const CATEGORIES = ['Road', 'Mountain', 'Hybrid', 'Cruiser', 'City', 'BMX'];
    public const GENDERS = ['Mens', 'Womens', 'Unisex'];
    public const CONDITION_OPTIONS = [
        1 => 'Beat up',
        2 => 'Decent',
        3 => 'Good',
        4 => 'Great',
        5 => 'Like New'
    ];
    public $brand;
    public $model;
    public $year;
    public $category;
    public $color;
    public $description;
    public $gender;
    public $price;
    public $weight_kg;
    public $condition_id;

    public function __construct($args = [])
    {
        $this->brand = $args['brand'] ?? '';
        $this->model = $args['model'] ?? '';
        $this->year = $args['year'] ?? '';
        $this->category = $args['category'] ?? '';
        $this->color = $args['color'] ?? '';
        $this->description = $args['description'] ?? '';
        $this->gender = $args['gender'] ?? '';
        $this->price = $args['price'] ?? 0;
        $this->weight_kg = $args['weight_kg'] ?? 0.0;
        $this->condition_id = $args['condition_id'] ?? 3;
    }
}

Here is the code I have so far to extract the CSV and display it in HTML

if (($open = fopen("../private/used_bicycles.csv", "r")) !== false) {
    while (($data = fgetcsv($open, 1000, ",")) !== false) {
        $array[] = $data;
    }
    fclose($open);
}

$args = [
    'brand', 'model', 'year',
    'category', 'gender', 'color',
    'weight_kg', 'condition_id', 'price'
];
$bike = new Bicycle($args);
?>
<table>
    <?php foreach ($array as $args) { ?>
        <tr>
            <td><?php echo h($bike->brand); ?></td>
            <td><?php echo h($bike->model); ?></td>
            <td><?php echo h($bike->year); ?></td>
            <td><?php echo h($bike->category); ?></td>
            <td><?php echo h($bike->gender); ?></td>
            <td><?php echo h($bike->color); ?></td>
            <td><?php echo h($bike->weight_kg()); ?></td>
            <td><?php echo h($bike->condition()); ?></td>
            <td><?php echo h($bike->price); ?></td>
        </tr>
    <?php } ?>
</table>

But nothing is showing up on the table. I am sorry if my explanation doesn't make sense. I am struggling with this course.

CSV:

Trek,Emonda,2017,Hybrid,Unisex,black,1.5,5,1495.00

Cannondale,Synapse,2016,Road,Unisex,matte black,1.0,5,1999.00

Schwinn,Cutter,2016,City,Unisex,white,18,4,450.00

Mongoose,Switchback Sport,2015,Mountain,Mens,blue,24,2,399.00

Diamondback,Overdrive,2016,Mountain,Unisex,dark green,23.7,3,565.00

Schwinn,21-Speed Suburban CS,2015,Hybrid,Womens,burgundy,20,3,299.00

Schwinn,Sanctuary 7-Speed,2016,Cruiser,Womens,purple,19.5,3,190.00

Vilano,Forza,2015,Road,Unisex,silver,13.6,4,390.00

SE,Creature,2016,BMX,Mens,dark grey,9.1,2,410.00



Solution 1:[1]

You need to create a Bicycle instance for every row. Here i assume your CSV fields have the same order as in the $args array;

$args = ['brand', 'model', 'year', 'category', 'gender', 'color', 'weight_kg', 'condition_id', 'price' ];
?>
<table>
  <?php foreach($array as $row_array) { 

    $bike = new Bicycle( array_combine($args, $row_array) );

    ?> 
    <tr>
      <td><?php echo h($bike->brand); ?></td>
      <td><?php echo h($bike->model); ?></td>
      <td><?php echo h($bike->year); ?></td>
      <td><?php echo h($bike->category); ?></td>
      <td><?php echo h($bike->gender); ?></td>
      <td><?php echo h($bike->color); ?></td>
      <td><?php echo h($bike->weight_kg()); ?></td>
      <td><?php echo h($bike->condition()); ?></td>
      <td><?php echo h($bike->price); ?></td>
    </tr>

  <?php } ?>

</table>

Full source (the csv file is named test.csv):

<?php
class Bicycle
{

    public const CATEGORIES = ['Road', 'Mountain', 'Hybrid', 'Cruiser', 'City', 'BMX'];
    public const GENDERS = ['Mens', 'Womens', 'Unisex'];
    public const CONDITION_OPTIONS = [
        1 => 'Beat up',
        2 => 'Decent',
        3 => 'Good',
        4 => 'Great',
        5 => 'Like New'
    ];
    public $brand;
    public $model;
    public $year;
    public $category;
    public $color;
    public $description;
    public $gender;
    public $price;
    public $weight_kg;
    public $condition_id;

    public function __construct($args = [])
    {
        $this->brand = $args['brand'] ?? '';
        $this->model = $args['model'] ?? '';
        $this->year = $args['year'] ?? '';
        $this->category = $args['category'] ?? '';
        $this->color = $args['color'] ?? '';
        $this->description = $args['description'] ?? '';
        $this->gender = $args['gender'] ?? '';
        $this->price = $args['price'] ?? 0;
        $this->weight_kg = $args['weight_kg'] ?? 0.0;
        $this->condition_id = $args['condition_id'] ?? 3;
    }

    function weight_kg(){
      return $this->weight_kg;
    }
    function condition(){
      return self::CONDITION_OPTIONS[$this->condition_id];
    }
}

$array = [];
if (($open = fopen("test.csv", "r")) !== false) {
    while (($data = fgetcsv($open, 1000, ",")) !== false) {
        $array[] = $data;
    }
    fclose($open);
}

// Don't know what that should be doing, so it just returns its argument.
function h( $what ){
  return $what;
}


$args = ['brand', 'model', 'year', 'category', 'gender', 'color', 'weight_kg', 'condition_id', 'price' ];
?>
<table>
  <?php foreach($array as $row_array) { 

    $bike = new Bicycle( array_combine($args, $row_array) );

    ?> 
    <tr>
      <td><?php echo h($bike->brand); ?></td>
      <td><?php echo h($bike->model); ?></td>
      <td><?php echo h($bike->year); ?></td>
      <td><?php echo h($bike->category); ?></td>
      <td><?php echo h($bike->gender); ?></td>
      <td><?php echo h($bike->color); ?></td>
      <td><?php echo h($bike->weight_kg()); ?></td>
      <td><?php echo h($bike->condition()); ?></td>
      <td><?php echo h($bike->price); ?></td>
    </tr>

  <?php 


  } ?>

</table>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1