= htmlspecialchars($product->name); ?>
+= htmlspecialchars($product->price); ?>€
+diff --git a/src/app/controllers/ProductController.php b/src/app/controllers/ProductController.php
index a8ce595..0f2b4b2 100644
--- a/src/app/controllers/ProductController.php
+++ b/src/app/controllers/ProductController.php
@@ -15,6 +15,18 @@ public function viewProducts() {
require_once __DIR__ . "/../views/products.php";
}
+
+ public function filterProducts() {
+ $data = json_decode(file_get_contents("php://input"), true);
+
+ $search = $data['search'] ?? '';
+ $sort = $data['sort'] ?? '';
+
+ $products = $this->productModel->getFiltered($search, $sort);
+
+ require __DIR__ . "/../views/partials/product_list.php";
+ }
+
public function viewSingleProduct() {
if($_SERVER['REQUEST_METHOD'] !== 'GET') {
header('Location: /products');
diff --git a/src/app/models/Product.php b/src/app/models/Product.php
index 021b661..d6a76f5 100644
--- a/src/app/models/Product.php
+++ b/src/app/models/Product.php
@@ -92,6 +92,31 @@ public function getProductById($id) {
return $stmt->fetch(PDO::FETCH_OBJ);
}
+ public function getFiltered($search, $sort) {
+ $query = "SELECT * FROM product p WHERE 1";
+
+ if(!empty($search)) {
+ $query .= " AND name like :search";
+ }
+
+ if(!empty(search)) {
+ if($sort == "name_desc") {
+ $query .= " ORDER BY name DESC";
+ } else if ($sort == "name_asc") {
+ $query .= " ORDER BY name ASC";
+ }
+ }
+
+
+ $stmt = $this->conn->prepare($query);
+ if(!empty($search)) {
+ $stmt->bindValue(":search", "%$search%");
+ }
+
+ $stmt->execute();
+ return $stmt->fetchAll(PDO::FETCH_OBJ);
+ }
+
/**
* Reusable product details used by admin edit forms.
*/
diff --git a/src/app/views/partials/product_list.php b/src/app/views/partials/product_list.php
new file mode 100644
index 0000000..91b82a7
--- /dev/null
+++ b/src/app/views/partials/product_list.php
@@ -0,0 +1,16 @@
+
+ image) ? $product->image : '/assets/images/test.jpg'; ?>
+ = htmlspecialchars($product->price); ?>€
+
+
= htmlspecialchars($product->name); ?>
+
price); ?>€
-