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->name); ?> + +
+

name); ?>

+

price); ?>€

+
+
+
+ diff --git a/src/app/views/products.php b/src/app/views/products.php index ef6b719..d1db5f5 100644 --- a/src/app/views/products.php +++ b/src/app/views/products.php @@ -4,24 +4,50 @@

Product list

-
- - image) ? $product->image : '/assets/images/test.jpg'; ?> -
- - <?php echo htmlspecialchars($product->name); ?> - -
-

name); ?>

-

price); ?>€

-
-
-
- +
+ + +
+ +
+
- \ No newline at end of file + + + + diff --git a/src/public/assets/css/style.css b/src/public/assets/css/style.css index 10240ee..b37e355 100644 --- a/src/public/assets/css/style.css +++ b/src/public/assets/css/style.css @@ -14,3 +14,52 @@ color: white !important; transition: all 0.3s ease; } + +h1 { + padding: 10px; +} + +.products { + display:flex; + flex-wrap: wrap; + justify-content: space-around; +} + +.product-item { + margin: 25px; + width: 250px; + height: 350px; + display: flex; + flex-direction: column; + box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); + transition: transform 0.3s ease-in-out; + +} + +.product-item:hover { + transform: scale(1.05); +} + +.product-item a { + width: 100%; + height: 100%; + display: block; + text-decoration: none; + color: #222; +} + +.product-item img { + height: 70%; + width: 100%; + object-fit: cover; +} + +.product-info { + width: 100%; + padding: 10px; +} + +.sort-filter { + padding-left: 10px; + padding-bottom:25px; +} diff --git a/src/routes/web.php b/src/routes/web.php index 18a907a..09c6563 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -153,6 +153,11 @@ 'action' => 'updateAccount', 'access' => ['client'] ], + '/products/filter' => [ + 'controller' => 'ProductController', + 'action' => 'filterProducts', + 'access' => ['guest', 'client'] + ], '/cart' => [ 'controller' => 'CartController', 'action' => 'cartAction',