-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.php
More file actions
139 lines (127 loc) · 3.57 KB
/
cart.php
File metadata and controls
139 lines (127 loc) · 3.57 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
<?php
require_once "templates/page_setup.php";
$_final;
$page_name = "cart";
include "templates/header.php";
include "templates/jumbotron.php";
?>
<?php
class cart{
public $name;
public $cost;
public $teamURL;
public function __construct($name="",$cost=0,$teamURL=""){
$this->name=$name;
$this->cost=$cost;
$this->teamURL=$teamURL;
}
}
?>
<?php
function total (){
$total = 0.0;
$row = $_SESSION['array'];
foreach ($row as $ing){
$total += $_SESSION['items'][$ing->name]['Total'];
}
return $total;
}
?>
<?php
function view_cart(){
$row = $_SESSION['array'];
echo '<table class="table">';
echo '<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Site</th>
</tr> ';
foreach ($row as $ing){
$price = number_format((float)$_SESSION['items'][$ing->name]['Total'], 2 , '.' , ''); // formatted to 2 decimals
$quant = $_SESSION['items'][$ing->name]['Quantity']; //quantity
echo '<tr>';
echo "<td>$ing->name</td>";
echo "<td>$price</td>";
echo "<td>$quant</td>";
echo "<td><a href=".$ing->teamURL.">$ing->teamURL</a></td>";
echo "<td><a href=\"cart.php?remove=yes&name=$ing->name&cost=$ing->cost&team=$ing->teamURL\">Remove from Cart</a></td>";
echo '</tr>';
}
echo '</table>';
}
function remove_item($name){
foreach($_SESSION['array'] as $itemsKey => $items){
foreach($items as $valueKey => $value){
if($valueKey == 'name' && $value == $name){
//delete this particular object from the $array
unset($_SESSION['array'][$itemsKey]);
}
}
}
if (empty($_SESSION['array']))
unset($_SESSION['array']);
}
?>
<?php
if (isset($_GET['remove'])):
$name = strip_tags($_GET['name']);
$cost = strip_tags($_GET['cost']);
$team = strip_tags($_GET['team']);
$sing = new cart($name,$cost,$team);
$quant = $_SESSION['items'][$name]['Quantity'];
if ($quant <= 1):
unset($_SESSION['items'][$name]); //remove
remove_item($name);//unset($_SESSION['array']);
unset($_GET['name']);
else:
$_SESSION['items'][$name]['Quantity']--; //remove only 1
$_SESSION['items'][$name]['Total'] -= $sing->cost;
endif;
header('Location: cart.php');
die();
endif;
?>
<?php
if (!isset($_SESSION['array']) and !isset($_GET['name'])):
echo '<h3 align="center">Your Cart is Empty</h3>';
echo '<p align="center"><a href="food.php">Continue Shopping</a></p>';
require 'templates/footer.php';
die();
endif;
?>
<?php
if (isset($_GET['name'])):
$name = strip_tags($_GET['name']);
$team = strip_tags($_GET['team']);
$cost = doubleval($_GET['cost']);
$sing = new cart($name,$cost,$team);
if (!isset($_SESSION['array'])):
$row = array(); //stores ingredients in cart
$row[] = $sing;
$_SESSION['array'] = $row;
$_SESSION['items'] = array();
$_SESSION['items'][$name] = array('Quantity' => 1, 'Total' => $row[0]->cost);
else:
$row = $_SESSION['array'];
if (isset($_SESSION['items'][$name])): //item exists already
$_SESSION['items'][$name]['Quantity']++;
$price = $sing->cost;
$_SESSION['items'][$name]['Total'] += $price;
else:
//create a new item
$_SESSION['items'][$name] = array('Quantity' => 1, 'Total' => $sing->cost);
$row[] = $sing; //add to cart
$_SESSION['array'] = $row;
endif;
endif;
endif;
?>
<div class="cart">
<h2 align="center">My Cart</h2>
<?php view_cart(); ?>
<h3 align="center">Your Total is: $<?php $_final = total(); echo number_format((float)$_final, 2 , '.' , '');?></h3>
<p align="center"><a href="checkout.php?total=<?php echo $_final;?>">Checkout</a></p>
<p align="center"><a href="products.php">Continue Shopping</a></p>
</div>
<?php require 'templates/footer.php';?>