This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.php
194 lines (145 loc) · 5.23 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
<?php include "session.php"; ?>
<?php include_once "./models/DatabaseLink.php"; ?>
<?php
/*
*cart.php
*Displays the cart that is stored
*
*/
/* Connect to database */
//Connect to the database
$db = new DatabaseLink();
$con = $db->connection;
$query = "";
$row = array();
//variables
$curU = $_SESSION['accountId'];
$cart = array();
$quanities = array();
$size = 0;
$total =0;
$outofstock = 0;
//fetch the cart for the user
$query = ("SELECT product_id FROM `cart_items` WHERE account_id = '$curU' " );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
//$row = mysql_fetch_array($result);
//fetch the item ids and the size
while($row = mysql_fetch_array($result))
{
$cart[$size] = $row[0];
$size++;
}
//if size is 0, the cart is empty, else display cart
if($size == 0)
{
echo("<h4>Cart Is Empty</h4>");
}
else
{
//display headers
echo("<form method =\"post\" action=\"updateCart.php\">");
echo("<font size = 5><u><b> Shopping Cart </u/b><br><br></font>");
//display each item of the cart
for($i = 0; $i < $size; $i++)
{
if($size != 0)
{
//fetch the amount an item from the cart
$query = ("SELECT amount FROM `cart_items` WHERE account_id = '$curU' AND product_id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$quanity = $row[0];
//fetch the file info from the db
$query = ("SELECT name, price, inventory FROM `products` WHERE id=" . $cart[$i] );
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
$name = $row[0];
$price = $row[1];
$quanityLeft = $row[2];
//fetch the image
$query = ("SELECT file_data FROM `images` WHERE product_id =" . $cart[$i]);
$result = mysql_query($query, $con) or die("Could not execute query '$query'");
$row = mysql_fetch_array($result);
//display the image, name, and price
echo("
<fieldset>
<img src=\"data:image/jpeg;base64," . base64_encode( $row[0] ) . "\" width=\"75\" height=\"75\">
<label><a href=\"productPage.php?product_id=". $cart[$i] . "\">$name</a><br>");
echo("Price: $" . $price . " <br>");
//if an item is out of stock, mark it as soo, and set the boolean to true.
if($quanityLeft == 0)
{
echo("<font color=\"red\">Out of Stock </font><style=\"margin-top:20px\"><a href=\"removeFromCart.php?id=" . $cart[$i] . "\"><button class=\"btn btn-small\" type=\"button\">remove</button></a>
");
$outofstock = 1;
}
else
{
//doesnt allow a user to have more than the inventory, prints out an appropriate error
if($quanity > $quanityLeft)
{
//echo("<font color=\"red\">Update exceeds stock</font>");
$quanities[$i] = "<input class=\"input-large\" type=\"text\" value=\"" . $quanity . "\" name=\"$cart[$i]\">";
echo($quanities[$i]);
echo("
<style=\"margin-top:20px\"><a href=\"removeFromCart.php?id=" . $cart[$i] . "\"><button class=\"btn btn-small\" type=\"button\">remove</button></a>
<font color=\"red\">Quanity cannot exceed stock, only " . $quanityLeft . " left.</font>
</label>
</fieldset>
");
//echo("");
$outofstock = 1;
}
else
{
$quanities[$i] = "<input class=\"input-mini\" type=\"text\" value=\"" . $quanity . "\" name=\"$cart[$i]\">";
echo($quanities[$i]);
echo("
<style=\"margin-top:20px\"><a href=\"removeFromCart.php?id=" . $cart[$i] . "\"><button class=\"btn btn-small\" type=\"button\">remove</button></a>
</label>
</fieldset>
");
}
}
//adds to the total price
$total = $total + $quanity * $price;
echo("<hr><br>");
}
}
if($outofstock == 0)
echo("<input type=\"submit\" value=\"Update\" /></form><hr>");
else
echo("<input type=\"submit\" value=\"Update\" /><span class=\"label label-warning\">will take all invalid items out of cart</span></p></form><hr>");
echo("Subtotal: $" . number_format($total, 2, '.', '') ."<br>");
//clears all the session variables.
$_SESSION['progress'] = -1;
$_SESSION['checkoutError'] = "";
$_SESSION['billingName'] = "";
$_SESSION['shippingName'] = "";
$_SESSION['billingAddress'] = "";
$_SESSION['shippingAddress'] = "";
$_SESSION['billingCity'] = "";
$_SESSION['shippingCity'] = "";
$_SESSION['billingState'] = "";
$_SESSION['shippingState'] = "";
$_SESSION['billingZip'] = "";
$_SESSION['shippingZip'] = "";
$_SESSION['billingPhone'] = "";
$_SESSION['shippingPhone'] = "";
$_SESSION['creditname'] = "";
$_SESSION['creditnumber'] = "";
$_session['creditdate'] = "";
$_SESSION['creditcode'] = "";
$_SESSION['shipping'] = "";
//doesnt allow a user to checkout if there are out of stock items in the cart
if($outofstock == 0)
{
echo("<form method = \"post\" action =\"checkout.php\"><input type=\"submit\" value=\"Checkout\"></form>");
}
else
{
echo("<br><span class=\"label label-important\">Please remove invalid Items before trying to checkout</span></p>");
}
}
$db->disconnect();
?>