/
cart.php
265 lines (253 loc) · 7.27 KB
/
cart.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
<?php
/**
* cart.php
*
* The shopping cart for FestiveGear.
*
* @author John Le Mieux
* @version 1.0 05/31/09
*/
ob_start();
require("includes/db.php");
require("includes/formlib.php");
define('CART_TIME', 2592000);
define('MAX_QTY', 10);
main("Festive Gear Shopping Cart");
/**
* The structure of the page.
*
* $title The title of the page
*/
function main($title = "") {
$db = new DB();
if (isset($_REQUEST['add'])) {
$pid = $_REQUEST['add'];
addItem($db, $pid);
} else if (isset($_REQUEST['del'])) {
$pid = $_REQUEST['del'];
deleteItem($db, $pid);
} else if (isset($_REQUEST['update'])) {
$pid = $_REQUEST['update'];
updateItem($db, $pid);
}
$other = getJavaScript();
include("includes/header.php");
showContent($title, $db);
include("includes/footer.php");
}
/**
* Inserts JavaScript into the HTML header.
*/
function getJavaScript() {
return<<<SCRIPT
<script type="text/javascript">
// Deletes or updates quantity of an item.
function updateQty(item) {
itemId = item.name;
if (item.value == "delete") {
location.href = 'cart.php?del='+itemId;
} else {
newQty = item.options[item.selectedIndex].text;
location.href = 'cart.php?update='+itemId+'&qty='+newQty;
}
}
// Makes sure the user is logged in before checking out.
function checkout(isLoggedIn) {
location.href = (isLoggedIn) ? 'checkout.php' : 'login.php';
}
</script>
SCRIPT;
}
/**
* Returns the quantity of an item in the shopping cart.
*
* @return The quantity of the item
*/
function getQuantity() {
$qty = 1;
if (isset($_REQUEST['qty'])) $qty = $_REQUEST['qty'];
$qty = intval($qty);
if ($qty < 0) $qty = 0;
if ($qty > MAX_QTY) $qty = MAX_QTY;
return $qty;
}
/**
* Adds an item to the cart.
*
* @param $db A DB object for secure database queries
* @param $pid The product ID
*/
function addItem($db, $pid) {
$cartID = getCartId();
$qty = getQuantity();
$sql = "SELECT * FROM shoppingcarts
WHERE CartID='$cartID' AND ProductID=$pid";
$result = $db->query($sql);
$numRows = mysql_num_rows($result);
if ($numRows != 0) {
$qty = mysql_result($result, 0, 'Quantity');
$_REQUEST['qty'] = $qty + 1;
updateItem($db, $pid);
} else {
$sql = "SELECT price FROM products WHERE ID=$pid";
$result = $db->query($sql);
if (mysql_num_rows($result) == 0) return;
$price = mysql_result($result, 0, 0);
$sql = "INSERT INTO shoppingcarts
VALUES('$cartID', $pid, NOW(), $price, $qty)";
$db->query($sql);
}
}
/**
* Deletes an item from the shopping cart.
*
* $db A DB object for secure database operations
* $pid The product ID
*/
function deleteItem($db, $pid) {
$cartID = getCartID();
$sql = "DELETE FROM shoppingcarts
WHERE CartID='$cartID' AND ProductID=$pid";
$db->query($sql);
}
/**
* Updates the quantity of an item.
*
* @param $db A DB object for secure database operations
* @param $pid The product ID
*/
function updateItem($db, $pid) {
$cartID = getCartID();
$qty = getQuantity();
if ($qty <= 0) {
deleteItem($db, $pid);
} else {
$sql = "UPDATE shoppingcarts SET Quantity=$qty
WHERE CartID='$cartID' AND ProductID=$pid";
$db->query($sql);
}
}
/**
* Returns the Cart ID
*
* @return The Cart ID
*/
function getCartId() {
if (isset($_COOKIE["cartID"])) {
return $_COOKIE["cartId"];
} else {
if (!session_id()) {
session_start();
}
setcookie("cartId", session_id(), time() + CART_TIME);
return session_id();
}
}
/**
* Displays the main content of the page.
*
* @param $title The title of the page
* @param $db A DB object for secure database operations
*/
function showContent($title, $db) {
$cartID = getCartId();
$sql = "SELECT ID, name, products.price, Quantity
FROM shoppingcarts, products
WHERE ID=ProductID AND CartID='$cartID'";
$result = $db->query($sql);
echo "<h1>$title</h1>\n";
echo "<table>\n";
showHeading();
$total = 0;
while ($row = mysql_fetch_row($result)) {
list($id, $prodName, $price, $qty) = $row;
$total += $price * $qty;
showItem($id, $prodName, $price, $qty);
}
$total = "$".number_format($total, 2);
showFooter($total);
echo "</table>\n";
$url = (isset($_SESSION['user'])) ? "true" : "false";
echo "<button onclick=\"checkout($url)\">Checkout</button>\n";
$f = new FormLib();
?>
<p>Keep shopping</p>
<form action="products.php" method="post" id="categories">
<fieldset>
<legend>Select a category</legend>
<table>
<tr>
<td class="inputcell">
<?php
$list = array("All"=>"", "On the Lawn"=>"lawn",
"Back at Camp"=>"camping", "Jammin'"=>"jam",
"At the Lake"=>"water", "Artists' CDs"=>"cd");
echo $f->makeSelect('category', $list);
?>
</td>
</tr>
</table>
</fieldset>
<p><input type="submit" value="Submit" /></p>
</form>
<?php
}
/**
* Displays a single item in a row of the table.
*
* @param $id The product ID
* @param $prodName The name of the product
* @param $price The individual selling price of the item
* @param $qty The quantity of this item in the shopping cart
*/
function showItem($id, $prodName, $price, $qty) {
echo "<tr>\n";
echo "<td>$prodName</td>\n<td>$".number_format($price, 2)."</td>\n<td>";
$f = new FormLib();
$data = array();
for ($i = 1; $i <= MAX_QTY; $i++) {
$data[$i] = $i;
}
$other = 'onChange="updateQty(this)"';
$opt = $f->makeSelect($id, $data, $qty, $other);
echo "$opt</td>\n<td>$".number_format($price * $qty, 2)."</td>\n";
echo "<td>";
echo '<button name="'.$id.'" value="delete" onclick="updateQty(this)">';
echo "Delete</button>";
echo "</td>\n";
echo "</tr>\n";
}
/**
* Displays the table headers.
*/
function showHeading() {
echo<<<HTML
<tr style="font-family:verdana; font-size:1.5em; color:white">
<td style="background:blue;">
<strong>Product</strong>
</td>
<td style="background:blue;">
<strong>Price</strong>
</td>
<td style="background:blue;">
<strong>Quantity</strong>
</td>
<td style="background:blue;">
<strong>Total</strong>
</td>
<td style="background:blue;">
<strong>Delete Item?</strong>
</td>
</tr>
HTML;
}
/**
* Displays a footer for the table.
*
* @param $total The total selling price for this order
*/
function showFooter($total) {
echo "<tr>\n<td></td>\n<td></td>\n<td></td>\n";
echo "<td>Cart Total<br />\n$total</td>\n</tr>\n";
}
?>