So the ultimate goal is to print out products as objects in a table using functions from these classes, but the output is a table with the correct amount of rows/columns but they are blank. I've spent 3 hours trying to figure this out and I'm stumped. There's three files
Product.php
<?php
class Product
{
protected $product_id;
protected $product_name;
protected $product_price;
public function __construct($product_id,$product_name, $product_price = '')
{
$this->setProductID($product_id);
$this->setProductName($product_name);
$this->setProductPrice($product_price);
}
public function getProductID() {
return $this->product_id;
}
public function getProductName() {
return $this->product_name;
}
public function getProductPrice() {
return $this->product_price;
}
public function setProductID($product_id)
{
$this->product_id = $product_id;
}
public function setProductName($product_name)
{
$this->product_name= $product_name;
}
public function setProductPrice($product_price)
{
$this->product_price= $product_price;
}
}
ProductMapper.php
<?php
class ProductMapper
{
public function getProducts()
{
$dbConn = getDbConnection();
$stmt = $dbConn->prepare("SELECT * FROM product");
$stmt->execute();
$outArray = array();
while ($row = $stmt->fetch()) {
$outArray[] = new Product($row['product_id'], $row['product_name'], $row['product_cost']);
}
return $outArray;
}
}
and Index.php which is where it is outputted.
<?php
require('classes/functions.php');
require('classes/Product.php');
require('classes/ProductMapper.php');
$productMapper = new ProductMapper();
$rows = $productMapper->getProducts();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product</title>
</head>
<body>
<tbody>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>{$row->getProductID($this)}</td>";
echo "<td>{$row->getProductName($id)}</td>";
echo "<td>{$row->getProductPrice($id)}</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>