-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.php
255 lines (243 loc) · 8.29 KB
/
products.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
include_once "php-templates/classes/Product.php";
include_once 'php-templates/unsupported-functions/str_contains_function.php';
//appended string to the sql when search string contains something from the ff
function checkCategoryAndSize($str)
{
$appendToSQL = '';
if ($str != '') {
if (str_contains($str, 'eye mask')) {
$appendToSQL .= 'OR category = 1 ';
}
if (str_contains($str, 'pillow case')) {
$appendToSQL .= 'OR category = 2 ';
}
if (str_contains($str, 'shorts')) {
$appendToSQL .= 'OR category = 3 ';
}
if (str_contains($str, 'pajama')) {
$appendToSQL .= 'OR category = 4 ';
}
if (str_contains($str, 'nightdress')) {
$appendToSQL .= 'OR category = 5 ';
}
if (str_contains($str, 'loungewear')) {
$appendToSQL .= 'OR category = 6 ';
}
if (str_contains($str, 'free size')) {
$appendToSQL .= 'OR size = 0 OR size = 3 OR size = 4 OR size = 6 ';
}
if (str_contains($str, 'plus size')) {
$appendToSQL .= 'OR size = 1 OR size = 3 OR size = 5 OR size = 6 ';
}
if (str_contains($str, 'one size')) {
$appendToSQL .= 'OR size = 2 OR size = 4 OR size = 5 OR size = 6 ';
}
}
return $appendToSQL;
}
$page = "products";
if (!isset($_GET['filter']))
$_GET['filter'] = 'all';
else {
switch ($_GET['filter']) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 'sleepingessentials':
case 'sleepwear':
case 'all':
break;
default:
$_GET['filter'] = 'all';
}
}
if (!isset($_GET['search'])) {
$_GET['search'] = '';
}
$productsArr = [];
// get products from the db
include 'php-templates/dbconnect.php';
$searchStr = mysqli_real_escape_string($conn, $_GET['search']);
$whereStr = "
WHERE (id LIKE '%$searchStr%' OR
name LIKE '%$searchStr%' OR
description LIKE '%$searchStr%' " . checkCategoryAndSize(strtolower($searchStr)) . ")
";
$filterStr = $_GET['filter'] === 'sleepingessentials' ?
'AND size IS NULL' : ($_GET['filter'] === 'sleepwear' ?
'AND (size >= 0 AND size <= 6)' : ('AND category =' . $_GET['filter']));
$getProductsSql = "SELECT * FROM product $whereStr" . ($_GET['filter'] === 'all' ? '' : $filterStr);
// echo $getProductsSql;
$result = $conn->query($getProductsSql);
if ($result->num_rows > 0)
while ($row = $result->fetch_assoc())
array_push($productsArr, $row);
$conn->close();
// print_r($productsArr);
// create product objects
$prodCount = count($productsArr);
$productsObjArr = [];
if ($prodCount > 0) {
foreach ($productsArr as $key => $value) {
// echo "$key: " . $value['id'] . " " . $value['name'] . " " . $value['description'] . " " . $value['price'] . " " . $value['category'] . " " . $value['size'] . " " . $value['img'] . " ";
if ($value['size'] === null)
$product = new SleepingEssential($value['id'], $value['name']);
else {
$product = new SleepWear($value['id'], $value['name']);
$product->setSize($value['size']);
}
$product->setDetails([$value['price'], $value['img'], $value['category'], $value['description']]);
array_push($productsObjArr, $product);
}
}
// print_r($productsObjArr);
$filterTitle = '';
switch ($_GET['filter']) {
case 'all':
$filterTitle = 'All Products';
break;
case 'sleepwear':
$filterTitle = 'All Sleepwear';
break;
case 'sleepingessentials':
$filterTitle = 'All Sleeping Essentials';
break;
case 1:
$filterTitle = 'Sleeping Essentials - Eye Mask';
break;
case 2:
$filterTitle = 'Sleeping Essentials - Pillowcase';
break;
case 3:
$filterTitle = 'Sleepwear - Shorts';
break;
case 4:
$filterTitle = 'Sleepwear - Pajama';
break;
case 5:
$filterTitle = 'Sleepwear - Nightdress';
break;
case 6:
$filterTitle = 'Sleepwear - Loungewear';
break;
default:
$filterTitle = '';
break;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include 'php-templates/head.php'; ?>
<!-- Favicon -->
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<title>Products</title>
<!-- Core theme CSS (includes Bootstrap)-->
<?php include "css/products-css.php" ?>
<?php
//include "css/styles-css.php"
?>
</head>
<body>
<!-- Navigation-->
<?php include 'php-templates/navbar.php'; ?>
<!-- Header-->
<?php include 'php-templates/products-header.php'; ?>
<div class="row">
<div class="col-md-3">
</div>
<div class="col-md-9">
<h1 class="mt-4"><?php echo $filterTitle ?></h1>
</div>
</div>
<!-- body -->
<div class="row p-0 me-5">
<!-- Sidebar -->
<?php include 'php-templates/products-sidebar.php'; ?>
<!-- px-lg-6 ml-4 not in css -->
<!-- products -->
<div class="mt-4 col-md-9 col-12">
<div class="row gx-4 gx-lg-5 row-cols-2 row-cols-xl-4 p-0 m-0">
<?php
if ($prodCount > 0)
for ($i = 0; $i < $prodCount; $i++)
echo $productsObjArr[$i]->productDisplayStr($i);
else { ?>
<div class="card p-5 w-100 text-center mt-4">
<?php echo "No Products Available"; ?>
</div>
<?php }
?>
</div>
</div>
</div>
<!-- modal -->
<?php foreach ($productsObjArr as $i => $product) {
$prodName = $product->getName();
?>
<div id="<?php echo "product_modal$i" ?>" class="modal">
<div class="modal-content">
<div class="modal-header">
<span onclick="document.getElementById('<?php echo "product_modal$i" ?>').style.display='none'" class="close btn-close pull-right"></span>
</div>
<div class="modal-body m-3">
<h2 class="text-center"><?php echo $prodName ?></h2>
<div class="p-3 d-flex list-group-vertical card">
<div class="text-center m-3 w-100 ">
<img style="max-width: 60%; " src="<?php echo $product->getImg(); ?>" alt="<?php echo "$i$prodName" ?>" />
</div>
<div class="m-3">
<p><b>₱ <?php echo $product->getPrice(); ?></b></p>
<?php echo $product->getDescription() == null ? '' : '<p><b>Description:</b> <br />' . $product->getDescription() . ' </p>'; ?>
<p><b>Category:</b> <?php echo $product->getCategory(); ?></p>
<?php
try {
echo "<p><b>Size: </b>" . $product->getSize() . "</p>";
} catch (Error $e) {
}
?>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
<!-- Footer-->
<?php include 'php-templates/footer.php'; ?>
<script>
<?php
for ($i = 0; $i < $prodCount; $i++) {
echo "
const modal$i = document.getElementById('product_modal$i');
const productView$i = document.getElementById('product_view_$i')
productView$i.onclick = function() {
// alert('asdf$i')
modal$i.style.display = \"block\";
}
";
}
?>
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
<?php
for ($i = 0; $i < $prodCount; $i++) {
echo "
if (event.target == modal$i) {
modal$i.scrollTo(0,0);
modal$i.style.display = \"none\";
}
";
}
?>
}
// const butt = document.getElementById('product_view_0')
// butt.onclick = function() {
// alert('asdf')
// }
</script>
</body>
</html>