-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.php
164 lines (97 loc) · 4.54 KB
/
store.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
<?php
session_start();
if(isset($_SESSION['id']) && $_SESSION['id'] != ''){
if(isset($_POST['logout'])){
setcookie($_SESSION['id'],'', time() - 3600, "/");
setcookie($_SESSION['id'],'', time() - 3600, "/PHPCOURSE/phpshop");
session_unset();
session_destroy();
header("Location: login.php");
exit;
}
if (isset($_POST['buy']) && $_POST['product'] != ''){
if(isset($_COOKIE[$_SESSION['id']]) && !empty($_COOKIE[$_SESSION['id']])) {
$_SESSION['cart'] = json_decode($_COOKIE[$_SESSION['id']], true);
}
// Add the product to the cart if it's not already there
if (!in_array($_POST['product'], $_SESSION['cart'])) {
$_SESSION['cart'][] = $_POST['product'];
$jsonEncodedCart = json_encode($_SESSION['cart']);
setcookie($_SESSION['id'], $jsonEncodedCart, time() + 3600, "/");
setcookie($_SESSION['id'],'', time() - 3600, "/PHPCOURSE/phpshop");
echo $_SESSION['cart'];
header("Location: ".$_SERVER['PHP_SELF']);
}
}
if (isset($_POST['cart_id']) && isset($_POST['REMOVE']) && $_POST['cart_id'] != '') {
$_SESSION['cart'] = json_decode($_COOKIE[$_SESSION['id']], true);
// Find the index of the product in the $_SESSION['cart'] array
$index = array_search($_POST['cart_id'], $_SESSION['cart']);
// Remove the product from the $_SESSION['cart'] array
unset($_SESSION['cart'][$index]);
// Encode the updated $_SESSION['cart'] array into a JSON string
$jsonEncodedCart = json_encode($_SESSION['cart']);
// Update the corresponding cookie with the new JSON string
setcookie($_SESSION['id'], $jsonEncodedCart, time() + 3600);
header("Location: ".$_SERVER['PHP_SELF']);
}
}else{
header("Location: login.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>store</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="submit" name="logout" value="logout">
</form>
<table>
<?php
$conn = mysqli_connect('localhost','root','','storedb');
if($conn != false){
$result = mysqli_query($conn,"SELECT * FROM products");
$dbproducts = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach($dbproducts as $dbproduct){
echo '<tr>';
echo '<td>' . $dbproduct['title'] . '</td>';
echo '<td><img width="50" height="50" src="http://localhost:8080/PHPCOURSE/phpshop/images/' . $dbproduct['image'] . '"></td>';
echo '<td>' . $dbproduct['price'] . '</td>';
echo '<td> <form method="POST"> <input type="hidden" name="product" value="'.$dbproduct['id'].'"> <input type="submit" name="buy" value="buy"> </form></td>';
echo '</tr>';
}
mysqli_close($conn);
}else{
echo "DATABASE ERROR";
}
?>
</table>
<table>
<?php
if(isset($_COOKIE[$_SESSION['id']])){
$productArray = json_decode($_COOKIE[$_SESSION['id']]);
print_r($productArray);
}
$conn = mysqli_connect('localhost', 'root', '', 'storedb');
if($conn != false && isset($_COOKIE[$_SESSION['id']]) ){
foreach($productArray as $prod_id){
$Result = mysqli_query($conn, "SELECT * FROM products WHERE id = '$prod_id'");
$products = mysqli_fetch_all($Result, MYSQLI_ASSOC);
echo '<tr>';
echo '<td>' . $products[0]['title'] . '</td>';
echo '<td><img width="50" height="50" src="http://localhost:8080/PHPCOURSE/phpshop/images/' . $products[0]['image'] . '"></td>';
echo '<td>' . $products[0]['price'] . '</td>';
echo '<td> <form method="POST"> <input type="hidden" name="cart_id" value="'.$products[0]['id'].'"> <input type="submit" name="REMOVE" value="REMOVE"> </form></td>';
echo '</tr>';
}
mysqli_close($conn);
}
?>
</table>
</body>
</html>