/
checkout.php
159 lines (152 loc) · 4.55 KB
/
checkout.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
<?php
/**
* checkout.php
*
* Checks the user out of the Shopping Cart. The order is moved to the orders
* table. The database entry for the Shopping Cart is deleted. The cookies for
* the CartID and Username are deleted.
*
* @author John Le Mieux
* @version 1.0 05/31/09
*/
ob_start();
require_once "includes/formlib.php";
require_once "includes/db.php";
if (!session_id()) session_start();
main("Festive Gear Checkout");
/**
* The structure of the page.
*
* @param $title The title of the page
*/
function main($title = "") {
include("includes/header.php");
showContent($title);
include("includes/footer.php");
}
/**
* Displays the main content of the page.
*
* @param $title The title of the page
*/
function showContent($title) {
$db = new DB();
$cartID = getCartID();
$sql = "SELECT ID, name, products.price, quantity, date
FROM shoppingcarts, products
WHERE productID=ID AND CartID='$cartID'";
$result = $db->query($sql);
echo "<h1>$title</h1>\n";
echo "<table>\n";
showHeading();
$user = isset($_SESSION['user']) ? $_SESSION['user'] : "";
while ($row = mysql_fetch_row($result)) {
list($productId, $prodName, $price, $qty, $date) = $row;
$total += $price * $qty;
showItem($productId, $prodName, $price, $qty);
$sql = "INSERT INTO orders(username, date, status)
VALUES ('$user', '$date', 'ordered')";
$db->query($sql);
$sql = "INSERT INTO orderItems(orderID, productID, quantity, status)
VALUES ('LAST_INSERT_ID()', '$productId', '$qty', 'ordered')";
$db->query($sql);
$sql = "DELETE FROM shoppingcarts WHERE CartID='$cartID'";
$db->query($sql);
}
$total = "$".number_format($total, 2);
showFooter($total);
echo "</table>\n";
$sql = "SELECT fname, lname, address, city, state, zip, country
FROM customers, addresses
WHERE customers.username=addresses.username
AND customers.username='$user'";
$result = $db->query($sql);
$row = mysql_fetch_row($result);
list($fname, $lname, $address, $city, $state, $zip, $country) = $row;
echo "<p>This order will be shipped to</p>";
echo "<p>$fname $lname</p>";
echo "<p>$address</p>";
echo "<p>$city, $state $zip</p>";
echo "<p>$country</p>";
setcookie('cartID', '', time() - 86400, '/');
session_destroy();
}
/**
* Returns the Cart ID.
*
* @return The Cart ID
*/
function getCartID() {
if (isset($_COOKIE["cartId"])) {
return $_COOKIE["cartId"];
} else {
redirect("login.php");
}
}
/**
* Redirects the page given an incomplete URL.
*
* @param $url A relative or absolute URL
*/
function redirect($url) {
$url = trim($url);
$absURL = "Location: ";
if (substr($url, 0, 1) == "/") {
$absURL .= "http://".$_SERVER['HTTP_HOST'];
} elseif (strtolower(substr($url, 0, 7)) != "http://") {
$absURL .= "http://".$_SERVER['HTTP_HOST'];
$absURL .= dirname($_SERVER['PHP_SELF'])."/";
}
$absURL .= $url;
header($absURL);
die("Could not redirect");
}
/**
* Displays a single item in a row of the Shopping Cart.
*
* @param $id The Product ID
* @param $prodName The name of the product
* @param $price The individual selling price of the product
* @param $qty The quantity of the product
*/
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;
}
echo "</tr>\n";
}
/**
* Displays a header for the table.
*/
function showHeading() {
echo<<<HTML
<tr style="font-family:verdana; font-size:1.5em; color:white">
<td bgcolor="blue">
<strong>Product</strong>
</td>
<td bgcolor="blue">
<strong>Price</strong>
</td>
<td bgcolor="blue">
<strong>Quantity</strong>
</td>
<td bgcolor="blue">
<strong>Total</strong>
</td>
</tr>
HTML;
}
/**
* Displays a footer for the Shopping Cart table.
*
* @param $total The total price of the 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";
}
?>