-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderForm.php
318 lines (260 loc) · 10.8 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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace gazelleRunningSupplies;
use mysqli;
class DBConnect
{
public object $sqlConnection;
var $allProducts;
var $basket;
function __construct()
{
$sqlServer = "localhost";
$database = "gazellerunningsupplies";
$this->allProducts = array();
$this->sqlConnection = new mysqli($sqlServer, "sa", "sa", $database);
}
function RetrieveAllProducts()
{
$sqlComm = "SELECT * FROM tblProduct";
$sqlReturn = $this->sqlConnection->query($sqlComm);
if($sqlReturn->num_rows > 0)
{
while($row = $sqlReturn->fetch_assoc())
{
$item = new Product($row["productID"], $row["productName"], $row["price"], $row["stock"], $row["category"], $row["description"]);
$this->allProducts[] = $item;
}
}
}
function GetAllProducts()
{
return $this->allProducts;
}
function GetProduct(int $productID)
{
foreach($this->allProducts as $product)
{
if($product->GetID() == $productID)
{
return $product;
}
}
}
function InstantiateAndPopulateBasket()
{
$basketID = $_SESSION["basketID"];
$this->basket = new Basket($basketID);
$sqlComm = "SELECT * FROM tblBasketItem WHERE basketID='$basketID'";
$sqlReturn = $this->sqlConnection->query($sqlComm);
if($sqlReturn->num_rows > 0)
{
while($row = $sqlReturn->fetch_assoc())
{
$product = $this->GetProduct($row["productID"]);
$newBasketItem = new basketItem($row["basketItemID"],$product , $row["quantity"]);
$this->basket->AddProductToBasket($newBasketItem);
}
}
}
function GetBasketTotal()
{
$total = 0;
if($this->basket->GetAllItems() != null)
{
foreach($this->basket->GetAllItems() as $basketItem)
{
$total += $basketItem->GetProduct()->GetPrice()*$basketItem->GetQuantity();
}
}
echo '<h2>£'.number_format($total, 2).'</h2>';
}
function GetBasket()
{
return $this->basket;
}
}
class Product
{
private int $productID;
private string $productName;
private string $productImagePath;
private float $price;
private int $stock;
private string $category;
private string $description;
function __construct(int $productID, string $productName, float $price, int $stock, string $category, string $description)
{
$this->productID = $productID;
$this->productName = $productName;
$this->price = $price;
$this->stock = $stock;
$this->category = $category;
$this->description = $description;
$this->AddProductImagePath("/productImages/" . $this->productID);
}
function AddProductImagePath(string $image)
{
$this->productImagePath = $image;
}
function GetID()
{
return $this->productID;
}
function GetName()
{
return $this->productName;
}
function GetPrice()
{
return $this->price;
}
}
class Basket
{
private $basketItems;
private int $basketID;
function __construct(int $basketID)
{
$this->basketID = $basketID;
$this->basketItems = array();
}
public function AddProductToBasket(basketItem $newItem)
{
$this->basketItems[] = $newItem;
}
public function GetAllItems()
{
return $this->basketItems;
}
}
class basketItem
{
private int $basketItemID;
private Product $product;
private int $quantity;
function __construct(int $ID, Product $product, int $amount)
{
$this->basketItemID = $ID;
$this->product = $product;
$this->quantity = $amount;
}
function GetProduct()
{
return $this->product;
}
function GetQuantity()
{
return $this->quantity;
}
}
$dbConnect = new DBConnect;
$dbConnect->RetrieveAllProducts();
session_start();
$dbConnect->InstantiateAndPopulateBasket();
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gazelle Running Supplies - Order Form</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div id="navBar">
<div id="adminUserName">
</div>
<div id="adminLogin">
<a href="adminLogin.php">Admin Login</a>
</div>
</div>
<img id="logoImage" src="gazelleLogo.jpeg" />
<h1 id="header">Order Form</h1>
<div id="mainContent">
<div id="orderBreakdown">
<h2>Order Breakdown</h2>
<div id="orderItems">
<table id="orderBreakdownTable">
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Item Total</th>
</tr>
<?php
foreach($dbConnect->GetBasket()->GetAllItems() as $basketItem)
{
echo '<tr>
<td>'.$basketItem->GetProduct()->GetName().'</td>
<td>'.$basketItem->GetQuantity().'</td>
<td>£'.number_format($basketItem->GetProduct()->GetPrice()*$basketItem->GetQuantity(), 2).
'</td>
</tr>';
}
?>
</table>
</div>
<span id="orderTotalLabels">
<h2>Order Total:</h2>
<?php $dbConnect->GetBasketTotal()?>
</span>
</div>
<div id="customerDetails">
<h2>Customer Details</h2>
<?php
if(isset($_GET["invalid"]))
{
switch ($_GET["invalid"])
{
case "blankName":
echo '<p id="invalidDetails">*** Please enter a name. ***</p>';
break;
case "blankAddr":
echo '<p id="invalidDetails">*** Please enter address details. ***</p>';
break;
case "blankCity":
echo '<p id="invalidDetails">*** Please enter a city. ***</p>';
break;
case "blankCounty":
echo '<p id="invalidDetails">*** Please enter a county. ***</p>';
break;
case "blankPost":
echo '<p id="invalidDetails">*** Please enter a postcode. ***</p>';
break;
case "badPost":
echo '<p id="invalidDetails">*** Please enter a valid postcode. ***</p>';
break;
case "blankPhone":
echo '<p id="invalidDetails">*** Please enter a contact number. ***</p>';
break;
case "blankEmail":
echo '<p id="invalidDetails">*** Please enter an email address. ***</p>';
break;
}
}
?>
<form id="detailsForm" method="post" action="invoice.php">
<span class="inputAreas" id="customerNameInput"><label for="txtFullName">Full Name: </label><input name="txtFullName" required title="Please enter a name for this order"/></span>
<span class="inputAreas" id="address1Input"><label for="txtAddress1">Address Line 1: </label><input name="txtAddress1" required/></span>
<span class="inputAreas" id="address2Input"><label for="txtAddress2">Address Line 2: </label><input name="txtAddress2" required/></span>
<span class="inputAreas" id="cityInput"><label for="txtCity">City: </label><input name="txtCity" required/></span>
<span class="inputAreas" id="countyInput"><label for="txtCounty">County: </label><input name="txtCounty" required/></span>
<span class="inputAreas" id="postcodeInput"><label for="txtPostcode">Postcode: </label><input name="txtPostcode" required maxlength=8 /></span>
<span class="inputAreas" id="phoneInput"><label for="txtPhone">Contact Number: </label><input type="number" name="txtPhone" required maxlength=11 /></span>
<span class="inputAreas" id="emailInput"><label for="txtEmail">Email Address: </label><input type="email" name="txtEmail" required /></span>
</div>
<div id="customerActions">
<input type="submit" class="uiButton" value="Place Order"/>
</form>
<form method="post" action="index.php">
<input type="submit" class="uiButton" value="Amend Order" formnovalidate/>
</form>
<form method="post" action="index.php?action=cancel">
<input type="submit" class="uiButton" value="Cancel Order" formnovalidate/>
</form>
</div>
</div>
<footer>
<p>© Gazelle Running Supplies</p>
</footer>
<script src="myJavaScript.js"></script>
</body>
</html>