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

Commit 4e52ccc

Browse files
committed
unfuck products list
1 parent e956166 commit 4e52ccc

7 files changed

Lines changed: 58 additions & 28 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
require __DIR__ .'/../models/Product.php';
4+
5+
class ProductController {
6+
private $productModel;
7+
8+
public function __construct(PDO $dbConnection) {
9+
$this->productModel = new Product($dbConnection);
10+
}
11+
12+
public function viewProducts() {
13+
$title = "View Products";
14+
$products = $this->productModel->getAll();
15+
require_once __DIR__ . "/../views/products.php";
16+
}
17+
}

src/app/controllers/products.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/app/models/product.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,33 @@
22

33
class Product
44
{
5-
public $id;
6-
public $name;
7-
public $description;
8-
public $price;
9-
public $supplier_id;
10-
}
5+
6+
private $conn;
7+
8+
public function __construct(PDO $dbConnection) {
9+
$this->conn = $dbConnection;
10+
}
11+
12+
public function create($id, $name, $description, $price, $supplier_id) {
13+
$query = "INSERT INTO product (id, name, description, prince, supplier_id)
14+
VALUES (:id, :name, :description, :price, :supplier_id)";
15+
16+
$stmt = $this->conn->prepare($query);
17+
$stmt->bindValue(":id", $id);
18+
$stmt->bindValue(":name", $name);
19+
$stmt->bindValue(":description", $description);
20+
$stmt->bindValue(":price", $price);
21+
$stmt->bindValue(":supplier_id", $supplier_id);
22+
23+
return $stmt->execute();
24+
}
25+
26+
public function getAll() {
27+
$query = "SELECT * FROM product";
28+
29+
$stmt = $this->conn->prepare($query);
30+
31+
$stmt->execute();
32+
return $stmt->fetchAll(PDO::FETCH_OBJ);
33+
}
34+
}

src/app/views/cart.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
</div>";
1212
}
1313
?>
14-
</div>
14+
</div>

src/app/views/partials/navbar.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<li><a href="/">Home</a></li>
1212
<?php if ($userId) { ?>
1313
<li><a href="#">Order</a></li>
14+
<li><a href="/products">Products list</a></li>
1415
<li><a href="/account">My account</a></li>
1516
<li><a href="/basket">Cart</a></li>
1617
<li><a href="/sign-out">Sign out</a></li>
@@ -20,4 +21,4 @@
2021
<?php } ?>
2122
</ul>
2223
</div>
23-
</nav>
24+
</nav>

src/app/views/products.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
<?php require_once '../controllers/products.php' ?>
21
<?php include 'partials/header.php'; ?>
3-
<?php if (!isset($_SESSION['user'])): ?>
4-
<div role="alert">
5-
You have to be signed-in to view this page.
6-
</div>
7-
<?php include 'partials/footer.php'; ?>
8-
<?php exit; ?>
9-
<?php endif; ?>
102

113
<div>
124
<h1>Product list</h1>
135
<!--FIXME: fix category table, to organise products by category (those seen on the menu)-->
146
<?php
7+
158
foreach ($products as $product) {
169
echo "<div>
17-
<p>" . $product->name . "</p>
18-
<p>" . $product->price . "</p>
10+
<p>" . $product->name . " - " . $product->price . "€</p>
11+
12+
<!--
1913
<form method='POST' action='index.php'>
2014
<input type='hidden' name='product_id' value='" . $product->id . "'>
2115
<button type='submit' name='action' value='add'>Add to Cart</button>
2216
<button type='submit' name='action' value='remove'>Remove one</button>
2317
</form>
18+
-->
2419
</div>";
2520
}
2621
?>
2722
</div>
28-
<?php include 'partials/footer.php'; ?>
23+
<?php include 'partials/footer.php'; ?>

src/routes/web.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
'/sign-out' => ['AuthController', 'signOut'],
88
'/account' => ['AuthController', 'viewAccount'],
99
'/account-data' => ['AuthController', 'getAccountData'],
10+
'/products' => ['ProductController', 'viewProducts']
1011
],
1112
'POST' => [
1213
'/sign-in' => ['AuthController', 'signIn'],
1314
'/sign-up' => ['AuthController', 'signUp'],
1415
],
15-
];
16+
];

0 commit comments

Comments
 (0)