-
Notifications
You must be signed in to change notification settings - Fork 1
/
product.php
305 lines (261 loc) · 13.5 KB
/
product.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html lang="en-GB">
<?php include './php/head.php'; ?>
<body class="debug o f h d">
<?php
include './php/cart-item.php';
session_start();
//Connect to database
$conn = new mysqli("localhost", "f36im", "f36im", "f36im");
if ($conn->connect_error) {
//Fallback if unable to connect to database
include_once ('./php/error.php');
exit();
}
$product_id = $_GET["id"];
$product_color = $_GET["color"];
$product_size = $_GET["size"];
$product_qty = $_GET["quantity"];
$add_to_cart = isset($_GET["add"]);
if (!$product_id) {
$product_id = 1;
$add_to_cart = false;
}
$result;
$query = 'SELECT p.name, p.price, p.gender, p.category, p.discount, p.description, i.color, i.size, i.stock FROM products AS p, inventory AS i
WHERE p.id = ' . $product_id . ' AND p.id = i.productsID ORDER BY i.color ASC;';
$result = $conn->query($query);
if ($result && $result->num_rows < 1) {
//ProductID does not belong to database
$product_id = 1;
$query = 'SELECT p.name, p.price, p.gender, p.category, p.discount, p.description, i.color, i.size, i.stock FROM products AS p, inventory AS i
WHERE p.id = 1 AND p.id = i.productsID ORDER BY i.color ASC;';
$result = $conn->query($query);
}
if ($result) {
$num_rows = $result->num_rows;
if ($num_rows > 0) {
$row;
$inventory_arr = array();
$distinct_color = array();
$distinct_size = array();
//Fill up array of color, size, and stock combination
for ($i = 0; $i < $num_rows; $i++) {
$row = $result->fetch_assoc();
$color = strtolower($row["color"]);
$size = $row["size"];
$stock = $row["stock"];
if (!in_array($color, $distinct_color)) {
array_push($distinct_color, $color);
}
if (!in_array($size, $distinct_size)) {
array_push($distinct_size, $size);
}
if (!isset($inventory_arr[$color])) {
$inventory_arr[$color] = array();
}
$inventory_arr[$color][$size] = $stock;
}
$result->free();
//Get product information
$name = stripslashes($row["name"]);
$discount = $row["discount"];
$price = $row["price"];
$price_after_discount = (1 - $product_discount/(float)100) * $price;
$description = stripslashes($row["description"]);
$gender = $row["gender"];
$category = $row["category"];
if (!$product_color || !in_array($product_color, $distinct_color)) {
$product_color = $distinct_color[0];
$add_to_cart = false;
}
if (!$product_size || !in_array($product_size, $distinct_size)) {
$product_size = $distinct_size[0];
$add_to_cart = false;
}
if (!$product_qty || $product_qty < 1) {
$product_qty = 1;
$add_to_cart = false;
}
$outofstock = false;
if (!isset($inventory_arr[$product_color][$product_size]) || $product_qty > $inventory_arr[$product_color][$product_size]) {
$product_qty = $inventory_arr[$product_color][$product_size];
if ($product_qty < 1) {
$outofstock = true;
$add_to_cart = false;
}
}
echo ' <script type="text/javascript">
var inventory_arr = ' . json_encode($inventory_arr ) . ';
var selectedColor = "' . $product_color . '";
var selectedSize = "' . $product_size . '";
</script>';
//Add selected product to cart
if ($add_to_cart) {
if (!isset($_SESSION["cart"])) {
$_SESSION["cart"] = array();
}
$cart_item = new CartItem($product_id, $product_color, $product_size, $product_qty);
$cart_index = get_item_index_in_cart($cart_item, $_SESSION["cart"]);
if ($cart_index >= 0) {
$_SESSION["cart"][$cart_index]->quantity += $product_qty;
} else {
array_push($_SESSION["cart"], $cart_item);
}
}
include './php/nav.php';
//Product preview and thumbnails
$section_id = 'product-details';
echo ' <section id="' . $section_id . '" class="product-details">
<div class="container">
<div class="row">
<div class="one column u-p-zero--right">';
foreach ($distinct_color as $color_name) {
if ($color_name == $product_color) {
echo '<div class="product-thumbnails product-thumbnails--active">';
} else {
echo '<div class="product-thumbnails">';
}
$button_id = $section_id . '_button_' . $product_id . '_' . $color_name;
echo ' <input type="image" id="' . $button_id . '" src="./images/' . $product_id . '_' . $color_name . '.jpg" width="100%" onclick="pickColor(this)">
</div>';
}
//Container for color input
echo ' </div>
<div class="four column">
<div class="product-preview">
<img id="' . $section_id . '_img_' . $product_id . '" src="./images/' . $product_id . '_' . $product_color . '.jpg" width="100%">
</div>
</div>
<div class="three column">
<form class="product-filters">
<input type="hidden" name="id" value="' . $product_id . '">
<input type="hidden" name="add">
<div id="option--color" class="option-group">
<div class="option__header">
<h4>Select color</h4>
</div>
<div class="row">';
foreach($distinct_color as $color_name) {
echo ' <div class="six column u-p-zero">
<label for="color--' . $color_name . '" class="label label--checkbox">';
if ($color_name == $product_color) {
echo ' <input type="radio" name="color" class="input--checkbox" id="color--' . $color_name . '" value="' . $color_name . '" checked onchange="updateStock(this, \'color\')">';
} else {
echo ' <input type="radio" name="color" class="input--checkbox" id="color--' . $color_name . '" value="' . $color_name . '" onchange="updateStock(this, \'color\')">';
}
echo ucfirst($color_name) .
'</label>
</div>';
}
//Container for size input
echo ' </div>
</div>
<div id="option--size" class="option-group">
<div class="option__header">
<h4>Select size</h4>
<div class="header__button">
<a href="#">Size Chart</a>
</div>
</div>
<div class="row">';
foreach($distinct_size as $size) {
echo '<div class="six column u-p-zero">
<label for="size--' . $size . '" class="label label--checkbox">';
if ($size == $product_size) {
echo '<input type="radio" name="size" class="input--checkbox" id="size--' . $size . '" value="' . $size . '" checked onchange="updateStock(this, \'size\')">';
} else {
echo '<input type="radio" name="size" class="input--checkbox" id="size--' . $size . '" value="' . $size . '" onchange="updateStock(this, \'size\')">';
}
echo strtoupper($size) .
'</label>
</div>';
}
echo ' </div>
</div>';
//Container for quantity input + submit button
echo ' <div class="option--quantity" id="option--quantity"'. ($outofstock ? ' style="display:none;"' : '') .'>
<div>Quantity</div>
<input type="number" min="1" max="' . $inventory_arr[$product_color][$product_size] . '" name="quantity" class="input--text" id="product-quantity" value="' . ($product_qty > 0 ? $product_qty : 1) . '" oninput="updatePriceProduct(this)">
</div>
<button type="submit" class="button button--primary button--large option__button" id="button--addtobag"'. ($outofstock ? ' style="display:none;"' : '') . '>
Add to Bag ($<span id="product-price-subtotal"> ' .
number_format($price_after_discount,2)
. '</span>)
</button>
<span class="button button--primary button--large option__button" id="button--outofstock"' . ($outofstock ? '' : ' style="display:none;"') .'>Out of Stock</span>
</form>
</div>';
//Display product information
echo ' <div class="four column">
<div class="product-info">
<div class="product-info__name">
<h4 class="header">'. $name . '</h4>
</div>
<div class="product-info__id">
Product ID: ' . $product_id . '
</div>
<div class="product-info__price">
<span class="product-info__price--current" id="product-price-single">$' . number_format($price_after_discount,2) . '</span>';
if ($product_discount > 0) {
echo ' <span class="product-info__price--pre-discount">$' . number_format($price,2) . '</span>';
}
echo ' </div>
<div class="product-info__description">' .
nl2br($description) .
'</div>
</div>
</div>
</div>
</div>
</section>';
//Recommended items: related items in gender and category sorted randomly
$query = 'SELECT products.id, products.name, products.price, products.discount FROM products WHERE gender = "' . $gender . '" AND
category = "' . $category . '" AND products.id != ' . $product_id . ' ORDER BY RAND() LIMIT 0, 4;';
$result = $conn->query($query);
if ($result) {
$num_rows = $result->num_rows;
if ($num_rows > 0) {
echo ' <section id="collection--recommended">
<div class="container">
<div class="row">
<div class="twelve column">
<div class="collection__signifier"></div>
<h2 class="header collection__header"><a href="#">Recommended For You</a></h2>
</div>
</div>
<div class="row">';
for ($i = 0; $i < $num_rows; $i++) {
$row = $result->fetch_assoc();
$product_id = $row["id"];
$product_name = $row["name"];
$product_price = $row["price"];
$product_discount = $row["discount"];
echo ' <div class="three column">';
include './php/product.php';
echo ' </div>';
}
echo ' </div>
</div>
</section>';
}
$result->free();
}
} else {
//ProductID does not belong to database
include './php/nav.php';
echo 'No product is found with the given ID';
exit();
}
} else {
//Unable to query database for product information
include './php/nav.php';
include_once ('./php/error.php');
exit();
}
$conn->close();
include './php/footer.php';
?>
<script type="text/javascript" src='./js/global.js'></script>
</body>
</html>