-
Notifications
You must be signed in to change notification settings - Fork 1
/
orderForm.php
78 lines (68 loc) · 2.27 KB
/
orderForm.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
<title>Order Form</title>
<?php include "template.php";
/** @var $conn */
?>
<link rel="stylesheet" href="css/orderForm.css">
<h1 class="text-primary">Order Form</h1>
<?php
$status = "";
if (isset($_POST['Code']) && $_POST['Code'] != "") {
$code = $_POST['Code'];
$row = $conn->querySingle("SELECT * FROM products WHERE code='$code'", true);
$name = $row['ProductName'];
$price = $row['Price'];
$image = $row['Image'];
$id = $row['ProductID'];
$cartArray = array(
$code => array(
'id' => $id,
'productName' => $name,
'code' => $code,
'price' => $price,
'quantity' => 1,
'image' => $image)
);
// Debug Purposes
// echo '<pre>'; print_r($cartArray); echo '</pre>';
if (empty($_SESSION["ShoppingCart"])) {
$_SESSION["ShoppingCart"] = $cartArray;
$status = "<div class='box'>Product is added to your cart!</div>";
} else {
$array_keys = array_keys($_SESSION["ShoppingCart"]);
if (in_array($code, $array_keys)) {
$status = "<div class='box' style='color:red;'>Product is already added to your cart!</div>";
} else {
$_SESSION["ShoppingCart"] = array_merge(
$_SESSION["ShoppingCart"], $cartArray
);
$status = "<div class='box'>Product is added to your cart!</div>";
}
}
}
?>
<div class="message_box" style="margin:10px 0px;">
<?php echo $status; ?>
</div>
<?php
if (!empty($_SESSION["ShoppingCart"])) {
$cart_count = count(array_keys($_SESSION["ShoppingCart"]));
?>
<div class="cart_div">
<a href="cart.php"><img src="images/cart-icon.png"/> Cart<span>
<?php echo $cart_count; ?></span></a>
</div>
<?php
}
$result = $conn->query("SELECT * FROM Products");
while ($row = $result->fetchArray()) {
echo "<div class='product_wrapper'>
<form method ='post' action =''>
<input type='hidden' name='Code' value=" . $row['Code'] . " />
<div class='image'><img src='images/productImages/" . $row['Image'] . "' width='100' height='100'/></div>
<div class='name'>" . $row['ProductName'] . "</div>
<div class='price'>$" . $row['Price'] . "</div>
<button type='submit' class='buy'>Add to Cart</button>
</form>
</div>";
}
?>