-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
138 lines (131 loc) · 4.01 KB
/
cart.php
File metadata and controls
138 lines (131 loc) · 4.01 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
<?php
session_start();
if(isset($_GET['clear']))
{
if ($_GET[clear])
{
unset($_SESSION['CART']);
$_SESSION['MSGS'] = array('Your cart has been emptied.');
session_write_close();
header("location: cart.php");
exit();
}
}
if ( isset($_GET['del']) )
{
foreach($_SESSION['CART'] as $cart_item_ID => $cart_item)
{
if($cart_item['pd_id'] == $_GET['del']){
unset($_SESSION['CART'][$cart_item_ID]);
$_SESSION['MSGS'] = array('Item remove from your cart.');
session_write_close();
header("location: cart.php");
exit();
}
}
}
if(isset($_GET['add']) )
{
//Include database connection details
require_once('config.php');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
die("Cannot access db.");
}
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$product;
$res = mysql_query("SELECT `tbl_product`.*,`tbl_category`.`cat_name`
FROM `tbl_product`
INNER JOIN `tbl_category`
ON `tbl_product`.`cat_id`=`tbl_category`.`cat_id`
WHERE `pd_id`=".$_GET['add']." LIMIT 1");
$product = mysql_fetch_assoc($res);
if(!isset( $_SESSION['CART']) ) $_SESSION['CART'] = array();
if(!in_array($product, $_SESSION['CART']))
{
array_push($_SESSION['CART'], $product );
$_SESSION['MSGS'] = array('Item added to your cart.');
session_write_close();
header("location: cart.php");
exit();
}
else
{
$_SESSION['ERR_MSGS'] = array('Item is already added to your cart.');
session_write_close();
header("location: cart.php");
exit();
}
}// if GET is there
?>
<?php
include 'includes/header.php';
include 'includes/nav.php';
?>
<div id="main">
<header class="container">
<h3 class="page-header">Cart</h3>
</header>
<div class="container">
<?php if( count($_SESSION['CART']) > 0 ) { ?>
<div class="table-responsive">
<table class="table products-table">
<thead>
<tr>
<th>Preview</th>
<th>Name</th>
<th>Description</th>
<th class="text-center">Category</th>
<th width="100" class="text-center">Price</th>
<th class="text-center">Remove</th>
</tr>
</thead>
<tbody>
<?php
$_SESSION['total'] = 0;
foreach ($_SESSION['CART'] as $item) {
$_SESSION['total'] += $item['pd_price'];
?>
<tr>
<td><img style="max-width:140px;" src="img/uploads/<?php echo $item['pd_image'] ?>" alt="<?php echo $item['pd_name'] ?>"></td>
<td><?php echo $item['pd_name'] ?></td>
<td><?php echo $item['pd_description'] ? $item['pd_description'] : '<span class="text-muted">No description</span>'; ?></td>
<td class="text-center"><?php echo $item['cat_name'] ?></td>
<?php setlocale(LC_MONETARY,'en_US'); ?>
<td class="text-center">₹ <?php echo money_format('%!i', floatval($item['pd_price'])); ?></td>
<td class="text-center"><a href="cart.php?del=<?php echo $item['pd_id'] ?>"><span class="glyphicon glyphicon-trash" onclick="return confirm('Are you sure you want to delete this item from you cart?');"> </span></a></td>
</tr>
<?php
}
?>
<tr>
<td colspan="3"></td>
<td>
<h4>Total:</h4>
</td>
<td colspan="2" class="text-info">
₹ <?php echo money_format('%!i', floatval($_SESSION['total'])); ?>
</td>
</tr>
</tbody>
</table>
</div>
<div class="pull-right">
<a href="cart.php?clear=true" class="btn btn-default">Clear <span class="glyphicon glyphicon-shopping-cart"></span></a>
<a href="order.php" class="btn btn-primary">Place Order</a>
</div>
<?php
} // check count of cart
else
{
echo '<div class="alert alert-info">Oh no! Add something to your cart from the Store.</div>';
}
?>
</div>
</div>
<?php
include 'includes/footer.php';
?>