Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.

Commit bb6416e

Browse files
authored
Merge pull request #41 from TheRefraction/add-product-page
Add a product page
2 parents f4c3ac9 + 84af768 commit bb6416e

5 files changed

Lines changed: 67 additions & 12 deletions

File tree

src/app/controllers/ProductController.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,29 @@ public function viewProducts() {
1414
$products = $this->productModel->getAll();
1515
require_once __DIR__ . "/../views/products.php";
1616
}
17+
18+
public function viewSingleProduct() {
19+
if($_SERVER['REQUEST_METHOD'] !== 'GET') {
20+
header('Location: /products');
21+
exit;
22+
}
23+
24+
$id = isset($_GET['id']) ? $_GET['id'] : null;
25+
26+
if(!$id) {
27+
header('Location: /products');
28+
exit;
29+
}
30+
31+
$product = $this->productModel->getById($id);
32+
33+
if(!$product) {
34+
header('Location: /products');
35+
exit;
36+
}
37+
38+
$title = $product->name;
39+
40+
require_once __DIR__ . "/../views/product.php";
41+
}
1742
}

src/app/models/Product.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function __construct(PDO $dbConnection) {
1111

1212
public function create($id, $name, $description, $price, $supplier_id) {
1313
$query = "INSERT INTO product (id, name, description, prince, supplier_id)
14-
VALUES (:id, :name, :description, :price, :supplier_id)";
14+
VALUES (:id, :name, :description, :price, :supplier_id);";
1515

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

2626
public function getAll() {
27-
$query = "SELECT * FROM product";
27+
$query = "SELECT * FROM product;";
2828

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

3131
$stmt->execute();
3232
return $stmt->fetchAll(PDO::FETCH_OBJ);
3333
}
34+
35+
public function getById($id) {
36+
$query = "SELECT product.*, c.name AS category_name,
37+
s.name AS supplier_name, s.email AS supplier_email,
38+
s.phone AS supplier_phone
39+
FROM product
40+
INNER JOIN product_to_category ptc ON product.id = ptc.product_id
41+
INNER JOIN product_category c ON ptc.category_id = c.id
42+
INNER JOIN supplier s ON product.supplier_id = s.id
43+
WHERE product.id = :id;";
44+
$stmt = $this->conn->prepare($query);
45+
46+
$stmt->execute(['id' => $id]);
47+
return $stmt->fetch(PDO::FETCH_OBJ);
48+
}
3449
}

src/app/views/product.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php include 'partials/header.php'; ?>
2+
3+
<div>
4+
<?php
5+
echo "<h1>". $product->name . "</h1>";
6+
echo "<p>Price: " . $product->price . "€</p>";
7+
echo "<p>Description: " . $product->description . "</p>";
8+
echo "<p>Category: " . $product->category_name . "</p>";
9+
10+
if($product->supplier_name
11+
&& $product->supplier_phone
12+
&& $product->supplier_email) {
13+
14+
echo "<p>Supplier: "
15+
. $product->supplier_name
16+
. " - " . $product->supplier_email
17+
. " - " . $product->supplier_phone
18+
. "</p>";
19+
}
20+
?>
21+
</div>
22+
<?php include 'partials/footer.php'; ?>

src/app/views/products.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77

88
foreach ($products as $product) {
99
echo "<div>
10-
<p>" . $product->name . " - " . $product->price . "€</p>
11-
12-
<!--
13-
<form method='POST' action='index.php'>
14-
<input type='hidden' name='product_id' value='" . $product->id . "'>
15-
<button type='submit' name='action' value='add'>Add to Cart</button>
16-
<button type='submit' name='action' value='remove'>Remove one</button>
17-
</form>
18-
-->
10+
<p><a href='/product?id=" . $product->id . "'>" . $product->name . "</a> - " . $product->price . "€</p>
1911
</div>";
2012
}
2113
?>

src/routes/web.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
'/sign-out' => ['AuthController', 'signOut'],
88
'/account' => ['AuthController', 'viewAccount'],
99
'/account-data' => ['AuthController', 'getAccountData'],
10-
'/products' => ['ProductController', 'viewProducts']
10+
'/products' => ['ProductController', 'viewProducts'],
11+
'/product' => ['ProductController', 'viewSingleProduct'],
1112
],
1213
'POST' => [
1314
'/sign-in' => ['AuthController', 'signIn'],

0 commit comments

Comments
 (0)