Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/app/controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,29 @@ public function viewProducts() {
$products = $this->productModel->getAll();
require_once __DIR__ . "/../views/products.php";
}

public function viewSingleProduct() {
if($_SERVER['REQUEST_METHOD'] !== 'GET') {
header('Location: /products');
exit;
}

$id = isset($_GET['id']) ? $_GET['id'] : null;

if(!$id) {
header('Location: /products');
exit;
}

$product = $this->productModel->getById($id);

if(!$product) {
header('Location: /products');
exit;
}

$title = $product->name;

require_once __DIR__ . "/../views/product.php";
}
}
19 changes: 17 additions & 2 deletions src/app/models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function __construct(PDO $dbConnection) {

public function create($id, $name, $description, $price, $supplier_id) {
$query = "INSERT INTO product (id, name, description, prince, supplier_id)
VALUES (:id, :name, :description, :price, :supplier_id)";
VALUES (:id, :name, :description, :price, :supplier_id);";

$stmt = $this->conn->prepare($query);
$stmt->bindValue(":id", $id);
Expand All @@ -24,11 +24,26 @@ public function create($id, $name, $description, $price, $supplier_id) {
}

public function getAll() {
$query = "SELECT * FROM product";
$query = "SELECT * FROM product;";

$stmt = $this->conn->prepare($query);

$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_OBJ);
}

public function getById($id) {
$query = "SELECT product.*, c.name AS category_name,
s.name AS supplier_name, s.email AS supplier_email,
s.phone AS supplier_phone
FROM product
INNER JOIN product_to_category ptc ON product.id = ptc.product_id
INNER JOIN product_category c ON ptc.category_id = c.id
INNER JOIN supplier s ON product.supplier_id = s.id
WHERE product.id = :id;";
$stmt = $this->conn->prepare($query);

$stmt->execute(['id' => $id]);
return $stmt->fetch(PDO::FETCH_OBJ);
}
}
22 changes: 22 additions & 0 deletions src/app/views/product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php include 'partials/header.php'; ?>

<div>
<?php
echo "<h1>". $product->name . "</h1>";
echo "<p>Price: " . $product->price . "€</p>";
echo "<p>Description: " . $product->description . "</p>";
echo "<p>Category: " . $product->category_name . "</p>";

if($product->supplier_name
&& $product->supplier_phone
&& $product->supplier_email) {

echo "<p>Supplier: "
. $product->supplier_name
. " - " . $product->supplier_email
. " - " . $product->supplier_phone
. "</p>";
}
?>
</div>
<?php include 'partials/footer.php'; ?>
10 changes: 1 addition & 9 deletions src/app/views/products.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@

foreach ($products as $product) {
echo "<div>
<p>" . $product->name . " - " . $product->price . "€</p>

<!--
<form method='POST' action='index.php'>
<input type='hidden' name='product_id' value='" . $product->id . "'>
<button type='submit' name='action' value='add'>Add to Cart</button>
<button type='submit' name='action' value='remove'>Remove one</button>
</form>
-->
<p><a href='/product?id=" . $product->id . "'>" . $product->name . "</a> - " . $product->price . "€</p>
</div>";
}
?>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
'/sign-out' => ['AuthController', 'signOut'],
'/account' => ['AuthController', 'viewAccount'],
'/account-data' => ['AuthController', 'getAccountData'],
'/products' => ['ProductController', 'viewProducts']
'/products' => ['ProductController', 'viewProducts'],
'/product' => ['ProductController', 'viewSingleProduct'],
],
'POST' => [
'/sign-in' => ['AuthController', 'signIn'],
Expand Down