-
Notifications
You must be signed in to change notification settings - Fork 2
/
cart.php
150 lines (143 loc) · 4.06 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
<?php
$title = "Cart";
session_start();
require_once "./functions/database_functions.php";
require_once "./functions/cart_functions.php";
require "./template/header.php";
if(!isset($_SESSION['user']))
{
echo '<div class="alert alert-danger" role="alert">
You Need to <a href="Signin.php">Signin</a> First!
</div>';
}
else
{
$conn = db_connect();
if(isset($_POST['medserial']))
{
$med_serial = $_POST['medserial'];
}
if(isset($med_serial))
{
if(!isset($_SESSION['cart']))
{
// $_SESSION['cart'] is associative array that medserial => qty
$_SESSION['cart'] = array();
$_SESSION['total_items'] = 0;
$_SESSION['total_price'] = '0.00';
}
if(!isset($_SESSION['cart'][$med_serial]))
{
$_SESSION['cart'][$med_serial] = 1;
}
elseif(isset($_POST['cart']))
{
$_SESSION['cart'][$med_serial]++;
unset($_POST);
}
}
// if save change button is clicked , change the qty of each medserial
if(isset($_POST['save_change']))
{
foreach($_SESSION['cart'] as $serial =>$qty)
{
if($_POST[$serial] == '0')
{
unset($_SESSION['cart']["$serial"]);
}
else
{
$_SESSION['cart']["$serial"] = $_POST["$serial"];
}
}
}
}
$title = "Your shopping cart";
require "./template/header.php";
if(isset($_SESSION['cart']) && (array_count_values($_SESSION['cart'])))
{
$_SESSION['total_price'] = total_price($_SESSION['cart']);
$_SESSION['total_items'] = total_items($_SESSION['cart']);
?>
<form action="cart.php" method="post">
<table class="table">
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
<?php
foreach($_SESSION['cart'] as $serial => $qty){
$conn = db_connect();
$med = mysqli_fetch_assoc(getmedBySerial($conn, $serial));
?>
<tr>
<td><?php echo $med['med_name'] . " by " . $med['med_manufacturer']; ?></td>
<td><?php echo "Rs " . $med['med_price']; ?></td>
<td><input type="text" value="<?php echo $qty; ?>" size="2" name="<?php echo $serial; ?>"></td>
<td><?php echo "Rs " . $qty * $med['med_price']; ?></td>
</tr>
<?php } ?>
<tr>
<th> </th>
<th> </th>
<th><?php echo $_SESSION['total_items']; ?></th>
<th><?php echo "Rs " . $_SESSION['total_price']; ?></th>
</tr>
</table>
<button type="submit" class="btn btn-primary" name="save_change"><span class="glyphicon glyphicon-ok"></span> Save Changes</button>
</form>
<br/><br/>
<a href="checkout.php" class="btn btn-primary">Go To Checkout</a>
<a href="medicines.php" class="btn btn-primary">Continue Shopping</a>
<?php
}
else
{
echo "<h3 class=\"text-warning\" >Your cart is empty! Please make sure you add some Medicines in it!</h3>";
}
if(isset($_SESSION['user'])){
$customer=getCustomerIdbyEmail($_SESSION['email']);
$customerid=$customer['id'];
$query="SELECT * FROM cart join cartitems join medicines join customers
on customers.id='$customerid' and cart.customerid='$customerid' and cart.id=cartitems.cartid and cartitems.productid=medicines.med_serial";
$result=mysqli_query($conn,$query);
if(mysqli_num_rows($result)!=0)
{
echo ' <br><br><br><h4>Your Purchase History</h4><table class="table">
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Date</th>
</tr>';
for($i = 0; $i < mysqli_num_rows($result); $i++){
while($query_row = mysqli_fetch_assoc($result)){
echo '<tr>
<td>
<a href="medicine.php?medserial=';
echo $query_row['med_serial'];
echo '">';
echo '<img style="height:100px;width:80px"class="img-responsive img-thumbnail" src="./bootstrap/img/';
echo $query_row['med_image'];
echo '">';
echo ' </a>
</td>
<td>';
echo $query_row['quantity'];
echo '
</td>
<td>';
echo date("Y/m/d") ."<br>";
echo'
</td>
</tr>';
}
}
echo '</table>';
}
}
?>
<?php
if(isset($conn)){ mysqli_close($conn); }
require_once "./template/footer.php";?>