-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_cart.php
More file actions
148 lines (127 loc) · 4.56 KB
/
view_cart.php
File metadata and controls
148 lines (127 loc) · 4.56 KB
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
<?php #
session_start(); // Start the session.
// Set the page title and include the HTML header:
$page_title = 'View Your Shopping Cart';
include ('./includes/header.html');
// Check if the form has been submitted (to update the cart):
if (isset($_POST['submitted'])) {
// Change any quantities:
foreach ($_POST['qty'] as $k => $v) {
// Must be integers!
$pid = (int) $k;
$qty = (int) $v;
if ( $qty == 0 ) { // Delete.
unset ($_SESSION['cart'][$pid]);
} elseif ( $qty > 0 ) { // Change quantity.
$_SESSION['cart'][$pid]['quantity'] = $qty;
}
} // End of FOREACH.
} // End of SUBMITTED IF.
// Display the cart if it's not empty...
if (!empty($_SESSION['cart'])) {
// Retrieve all of the information for the prints in the cart:
require_once ('../mysqli_connect.php');
$q = "SELECT print_id, CONCAT_WS(' ', first_name, middle_name, last_name) AS artist, print_name FROM artists, prints WHERE artists.artist_id = prints.artist_id AND prints.print_id IN (";
foreach ($_SESSION['cart'] as $pid => $value) {
$q .= $pid . ',';
}
$q = substr($q, 0, -1) . ') ORDER BY artists.last_name ASC';
$r = mysqli_query ($dbc, $q);
// Create a form and a table:
echo '<!-- content -->
<div id="content">
<div class="container">
<div class="inside">
<!-- box begin -->
<div class="box alt">
<div class="left-top-corner">
<div class="right-top-corner">
<div class="border-top"></div>
</div>
</div>
<div class="border-left">
<div class="border-right">
<div class="inner">
';
echo '<form action="view_cart.php" method="post">
<table border="0" width="90%" cellspacing="3" cellpadding="3" align="center">
<tr>
<td align="left" width="30%"><b>Artist</b></td>
<td align="left" width="30%"><b>Print Name</b></td>
<td align="right" width="10%"><b>Price</b></td>
<td align="center" width="10%"><b>Qty</b></td>
<td align="right" width="10%"><b>Total Price</b></td>
</tr>
';
// Print each item...
$total = 0; // Total cost of the order.
while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) {
// Calculate the total and sub-totals.
$subtotal = $_SESSION['cart'][$row['print_id']]['quantity'] * $_SESSION['cart'][$row['print_id']]['price'];
$total += $subtotal;
// Print the row.
echo "\t<tr>
<td align=\"left\">{$row['artist']}</td>
<td align=\"left\">{$row['print_name']}</td>
<td align=\"right\">\${$_SESSION['cart'][$row['print_id']]['price']}</td>
<td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['print_id']}]\" value=\"{$_SESSION['cart'][$row['print_id']]['quantity']}\" /></td>
<td align=\"right\">$" . number_format ($subtotal, 2) . "</td>
</tr>\n";
} // End of the WHILE loop.
mysqli_close($dbc); // Close the database connection.
// Print the footer, close the table, and the form.
echo '
<tr>
<td colspan="4" align="right"><b>Total:</b></td>
<td align="right">$' . number_format ($total, 2) . '</td>
</tr>
</table>
<div align="center"><input type="submit" name="submit" value="Update My Cart" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form><p align="center">Enter a quantity of 0 to remove an item.
<br /><br /><a href="checkout.php">Checkout</a></p>';
} else {
echo '
<!-- content -->
<div id="content">
<div class="container">
<div class="inside">
<!-- box begin -->
<div class="box alt">
<div class="left-top-corner">
<div class="right-top-corner">
<div class="border-top"></div>
</div>
</div>
<div class="border-left">
<div class="border-right">
<div class="inner"> <p>Your cart is currently empty.</p>
</div>
</div>
</div>
<div class="left-bot-corner">
<div class="right-bot-corner">
<div class="border-bot"></div>
</div>
</div>
</div>
<!-- box end -->
</div>
</div>
</div>';
}
echo ' </div>
</div>
</div>
<div class="left-bot-corner">
<div class="right-bot-corner">
<div class="border-bot"></div>
</div>
</div>
</div>
<!-- box end -->
</div>
</div>
</div>';
include ('./includes/footer.html');
?>