source: Products.php

Last change on this file was e3d4e0a, checked in by Vlado 222039 <vlado.popovski@…>, 6 days ago

Upload project files

  • Property mode set to 100644
File size: 16.9 KB
Line 
1<?php
2
3 session_start();
4
5 require "./connect.php";
6
7
8 $category = htmlspecialchars($_GET["category"] ?? '');
9 $price_sort = htmlspecialchars($_GET['price-sort'] ?? '');
10 $unique_brands = null;
11
12 if(isset($_GET['submit']) && isset($_GET['product_id'])) {
13
14 if(!isset($_SESSION['user_ID'])) {
15 header("Location: ./Log In.php");
16 }
17
18 if($_GET['submit'] == 'add-to-wishlist') {
19
20 $check = mysqli_query($conn, "SELECT * FROM wishlist WHERE user_id={$_SESSION['user_ID']} AND product_id = {$_GET['product_id']};");
21
22 try {
23 if(mysqli_num_rows($check) <= 0) {
24 $res = mysqli_query($conn, "INSERT INTO wishlist(user_id, product_id) VALUES ({$_SESSION['user_ID']}, {$_GET['product_id']});");
25 }
26 } catch(Exception $e) {
27 echo $e;
28 }
29
30 header("Location: ./Wishlist.php");
31 }
32 else if($_GET['submit'] == 'add-to-cart') {
33
34 // proveri dali veke postoi
35 $check = mysqli_query($conn, "SELECT * FROM cart WHERE user_id={$_SESSION['user_ID']} AND product_id = {$_GET['product_id']};");
36 $quantity = 1;
37
38 if(isset($_GET['quantity'])) {
39 $quantity = $_GET['quantity'];
40 }
41
42 try {
43 if(mysqli_num_rows($check) >= 1) {
44 mysqli_query($conn, "UPDATE cart SET quantity = {$quantity} WHERE id = {$check->fetch_assoc()['id']};");
45 }
46 else {
47 $res = mysqli_query($conn, "INSERT INTO cart(user_id, product_id, quantity) VALUES ({$_SESSION['user_ID']}, {$_GET['product_id']}, {$quantity});");
48 }
49 } catch(Exception $e) {
50
51 }
52
53 header("Location: ./Cart.php");
54 }
55
56 die();
57 }
58
59 if(empty($category)) {
60 $category = 'All';
61 }
62
63 if(empty($price_sort) || ($price_sort != 'low-to-high' && $price_sort != 'high-to-low' && $price_sort != 'none')) {
64 $price_sort = 'none';
65 }
66
67 if($category != 'All' && $category != 'Monitor' && $category != 'CPU' && $category != 'GPU'
68 && $category != 'PSU' && $category != 'Motherboards' && $category != 'Cases'
69 && $category != 'Storage' && $category != 'Peripherals')
70 {
71 $category = 'All';
72 }
73
74 if($category == 'All') {
75 $unique_brands = mysqli_query($conn, "SELECT DISTINCT brand FROM products;");
76 }
77 else {
78 $unique_brands = mysqli_query($conn, "SELECT DISTINCT brand FROM products WHERE category = \"{$category}\";");
79 }
80?>
81
82
83<!DOCTYPE html>
84<html lang="en">
85<head>
86 <meta charset="UTF-8">
87 <meta name="viewport" content="width=device-width, initial-scale=1.0">
88 <title>Products</title>
89 <link rel="stylesheet" href="../CSS/Header.css">
90 <link rel="stylesheet" href="../CSS/Products.css">
91 <link rel="stylesheet" href="https://unpkg.com/boxicons@latest/css/boxicons.min.css">
92</head>
93<body>
94
95 <?php include './components/Header.html' ?>
96
97 <section>
98 <div class="container">
99 <form class="sidebar" action="./Products.php" method="GET">
100 <h1>Products</h1>
101 <h5>Category</h5>
102 <select id="selectCategory" name="category">
103 <option value="All" <?php if($category == "All") echo "selected"; ?> >All</option>
104 <option value="Monitor" <?php if($category == "Monitor") echo "selected"; ?> >Monitors</option>
105 <option value="CPU" <?php if($category == "CPU") echo "selected"; ?> >Processors</option>
106 <option value="GPU" <?php if($category == "GPU") echo "selected"; ?> >Graphics Cards</option>
107 <option value="PSU" <?php if($category == "PSU") echo "selected"; ?> >Power Supplies</option>
108 <option value="Motherboards" <?php if($category == "Motherboards") echo "selected"; ?> >Motherboards</option>
109 <option value="Storage" <?php if($category == "Storage") echo "selected"; ?> >Storage</option>
110 <option value="Peripherals" <?php if($category == "Peripherals") echo "selected"; ?> >Peripherals</option>
111 <option value="Cases" <?php if($category == "Cases") echo "selected"; ?> >Cases</option>
112 </select>
113
114 <h5>Sort By</h5>
115 <label for="sortPrice">Price</label>
116 <select id="sortPrice" name="price-sort">
117 <option value="none" <?php if($price_sort == 'none') echo "selected" ?> >None</option>
118 <option value="low-to-high" <?php if($price_sort == 'low-to-high') echo "selected" ?> >Low to High</option>
119 <option value="high-to-low" <?php if($price_sort == 'high-to-low') echo "selected" ?> >High to Low</option>
120 </select>
121
122 <h5>Filter By Brand</h5>
123 <div class="checkbox-group">
124 <?php
125 $valid_brands = [];
126
127 if(mysqli_num_rows($unique_brands) >= 1) {
128 foreach($unique_brands as $brand_list){
129 $checked_brand = [];
130
131 if(empty($brand_list['brand'])) {
132 continue;
133 }
134
135 if(isset($_GET['brands'])) {
136 $checked_brand = $_GET['brands'];
137
138 if(in_array($brand_list['brand'], $checked_brand)) {
139 $valid_brands[] = $brand_list['brand'];
140 }
141 }
142 ?>
143 <div>
144 <input type="checkbox" name="brands[]" value="<?php echo $brand_list['brand'] ?>" <?php if(in_array($brand_list['brand'], $checked_brand)) echo "checked"; ?> >
145 <label><?php echo $brand_list['brand'] ?></label>
146 </div>
147
148 <?php
149 }
150 }
151 ?>
152 </div>
153
154 <input type="submit" value="Filter" id="filter-search-button">
155 </form>
156
157 <div class="main-content">
158 <div class="products">
159 <?php
160 $all_products = null;
161
162 if(isset($_GET['brands'])) {
163
164 $brand_parameter = implode(',', $valid_brands);
165 $brand_parameter = explode(",", $brand_parameter);
166 $brand_parameter = "'".implode("','", $brand_parameter)."'";
167
168 if($category != 'All') {
169 if($price_sort == 'low-to-high' && count($valid_brands) >= 1) {
170 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\" AND brand IN ({$brand_parameter})
171 ORDER BY (price - (price*discount/100.0)) ASC;
172 ");
173 }
174 else if($price_sort == 'low-to-high' && count($valid_brands) <= 0) {
175 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\" ORDER BY (price - (price*discount/100.0)) ASC;");
176 }
177 else if($price_sort == 'high-to-low' && count($valid_brands) >= 1) {
178 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\" AND brand IN ({$brand_parameter})
179 ORDER BY (price - (price*discount/100.0)) DESC;
180 ");
181 }
182 else if($price_sort == 'high-to-low' && count($valid_brands) <= 0) {
183 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\" ORDER BY (price - (price*discount/100.0)) DESC;");
184 }
185 else if(count($valid_brands) >= 1) {
186 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\" AND brand IN ({$brand_parameter})
187 ;");
188 }
189 else {
190 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\";");
191 }
192 }
193 else {
194 if($price_sort == 'low-to-high') {
195 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE brand IN ({$brand_parameter})
196 ORDER BY (price - (price*discount/100.0)) ASC;
197 ");
198 }
199 else if($price_sort == 'high-to-low') {
200 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE brand IN ({$brand_parameter})
201 ORDER BY (price - (price*discount/100.0)) DESC;
202 ");
203 }
204 else {
205 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE brand IN ({$brand_parameter})
206 ;");
207 }
208 }
209
210 if(mysqli_num_rows($all_products) >= 1) {
211 foreach($all_products as $product) {
212 ?>
213 <div class="row">
214 <div class="image-container">
215 <img src="
216 <?php
217 $product_image = basename(strrchr($product['image1'], '/'));
218 $product_image = str_replace(array("'"), '', $product_image);
219 echo 'UPLOADED_IMAGES/'.$product_image;
220 ?>
221 ">
222 </div>
223 <div class="product-name">
224 <a href="./Product.php?product_id=<?php echo $product['product_id'] ?>">
225 <?php echo $product['name']; ?>
226 </a>
227 </div>
228 <div class="product-price">
229 $
230 <?php
231 $discount = $product['price'] * ($product['discount'] / 100);
232 echo $product['price'] - $discount;
233 ?>
234 </div>
235 <form class="icons-container" action="./Products.php" method="GET">
236 <input type="hidden" name="product_id" value="<?php echo $product['product_id'] ?>">
237 <button type="submit" name="submit" value="add-to-cart" style="border: none; font-size: 1.0em; background: transparent;" class='bx bx-cart'></button>
238 <button type="submit" name="submit" value="add-to-wishlist" style="border: none; font-size: 1.0em; background: transparent;" class='bx bx-heart'></button>
239 </form>
240 </div>
241 <?php
242 }
243 }
244 else {
245 echo '<p>No products</p>';
246 }
247 }
248 else {
249
250 if($category != 'All') {
251 if($price_sort == 'low-to-high') {
252 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\" ORDER BY (price - (price*discount/100.0)) ASC;");
253 }
254 else if($price_sort == 'high-to-low') {
255 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\" ORDER BY (price - (price*discount/100.0)) DESC;");
256 }
257 else {
258 $all_products = mysqli_query($conn, "SELECT * FROM products WHERE category = \"$category\";");
259 }
260 }
261 else {
262 if($price_sort == 'low-to-high') {
263 $all_products = mysqli_query($conn, "SELECT * FROM products ORDER BY (price - (price*discount/100.0)) ASC;");
264 }
265 else if($price_sort == 'high-to-low') {
266 $all_products = mysqli_query($conn, "SELECT * FROM products ORDER BY (price - (price*discount/100.0)) DESC;");
267 }
268 else {
269 $all_products = mysqli_query($conn, "SELECT * FROM products;");
270 }
271 }
272
273 if(mysqli_num_rows($all_products) >= 1) {
274 foreach($all_products as $product) {
275 ?>
276 <div class="row">
277 <div class="image-container">
278 <img src="
279 <?php
280 $product_image = basename(strrchr($product['image1'], '/'));
281 $product_image = str_replace(array("'"), '', $product_image);
282 echo 'UPLOADED_IMAGES/'.$product_image;
283 ?>
284 ">
285 </div>
286 <div class="product-name">
287 <a href="./Product.php?product_id=<?php echo $product['product_id'] ?>">
288 <?php echo $product['name']; ?>
289 </a>
290 </div>
291 <div class="product-price">
292 $
293 <?php
294 $discount = $product['price'] * ($product['discount'] / 100);
295 echo $product['price'] - $discount;
296 ?>
297 </div>
298 <form class="icons-container" action="./Products.php" method="GET">
299 <input type="hidden" name="product_id" value="<?php echo $product['product_id'] ?>">
300 <button type="submit" name="submit" value="add-to-cart" style="border: none; font-size: 1.0em; background: transparent;" class='bx bx-cart'></button>
301 <button type="submit" name="submit" value="add-to-wishlist" style="border: none; font-size: 1.0em; background: transparent;" class='bx bx-heart'></button>
302 </form>
303 </div>
304 <?php
305 }
306 }
307 else {
308 echo '<p>No products</p>';
309 }
310 }
311 ?>
312 </div>
313 </section>
314</body>
315</html>
Note: See TracBrowser for help on using the repository browser.