-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart2-action.php
106 lines (90 loc) · 2.35 KB
/
cart2-action.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
<?php
//index.php
$connect = new PDO("mysql:host=localhost;dbname=nnk_fods", "root", "");
$message = '';
if(!isset($_COOKIE["shopping_cart"]))
{setcookie('shopping_cart', json_encode([]), time() + (86400 * 30));}
if(isset($_POST["add_to_cart"]))
{
if(isset($_COOKIE["shopping_cart"]))
{
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
}
else { $cart_data = array(); }
$item_id_list = array_column($cart_data, 'item_id');
if(in_array($_POST["hidden_id"], $item_id_list))
{
foreach($cart_data as $keys => $values)
{
if($cart_data[$keys]["item_id"] == $_POST["hidden_id"])
{
$cart_data[$keys]["item_quantity"] = $cart_data[$keys]["item_quantity"] + $_POST["quantity"];
}
}
}
else
{
$item_array = array(
'item_id' => $_POST["hidden_id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"]
);
$cart_data[] = $item_array;
}
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30));
header("location:index.php?success=1");
}
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
foreach($cart_data as $keys => $values)
{
if($cart_data[$keys]['item_id'] == $_GET["id"])
{
unset($cart_data[$keys]);
$item_data = json_encode($cart_data);
setcookie("shopping_cart", $item_data, time() + (86400 * 30));
header("location:?remove=1");
}
}
}
if($_GET["action"] == "clear")
{
setcookie("shopping_cart", "", time() - 3600);
header("location:?clearall=1");
}
}
if(isset($_GET["success"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Item Added into Cart
</div>
';
}
if(isset($_GET["remove"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Item removed from Cart
</div>
';
}
if(isset($_GET["clearall"]))
{
$message = '
<div class="alert alert-success alert-dismissible">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Your Shopping Cart has been clear...
</div>
';
}
?>