質問編集履歴

1

すみません。コードが抜け落ちていたようです。

2020/12/05 06:08

投稿

fk0617
fk0617

スコア10

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,341 @@
18
18
 
19
19
  ```php
20
20
 
21
+ <?php
22
+
23
+
24
+
25
+ require_once('functions.php');
26
+
27
+
28
+
29
+ session_start();
30
+
31
+
32
+
33
+ $name = $_SESSION['name'];
34
+
35
+
36
+
37
+ if (isset($_SESSION['id'])) {
38
+
39
+ $msg = 'こんにちは、' . h($name) . ' さん';
40
+
41
+ $link = '<a href="logout.php">ログアウト</a>';
42
+
43
+ } else {
44
+
45
+ $msg = 'ようこそZAZATOWNへ';
46
+
47
+ $login_msg = 'ログインしてください';
48
+
49
+ $link = '<a href="login.php">ログイン</a>';
50
+
51
+ }
52
+
53
+
54
+
55
+ if (isset($_POST['cartIn'])) {
56
+
57
+
58
+
59
+ unset($_SESSION['favorite'][$_POST['product_id']]);
60
+
61
+
62
+
63
+ $product_id = $_POST['product_id'];
64
+
65
+
66
+
67
+ $_SESSION['cart'][$product_id] = [
68
+
69
+ 'product_id' => $_POST['product_id'],
70
+
71
+ 'productImage' => $_POST['productImage'],
72
+
73
+ 'brandName' => $_POST['brandName'],
74
+
75
+ 'productName' => $_POST['productName'],
76
+
77
+ 'price' => $_POST['price'],
78
+
79
+ 'size' => $_POST['size'],
80
+
81
+ 'count' => $_POST['count'] + $_SESSION['cart'][$product_id]['count'],
82
+
83
+ ];
84
+
85
+ $cartmsg = "カートに入りました";
86
+
87
+ header('Location: http://192.168.33.10:8000/cart.php');
88
+
89
+
90
+
91
+ $count = $_SESSION['cart'][$product_id]['count'];
92
+
93
+ $price = $_SESSION['cart'][$product_id]['price'];
94
+
95
+ $subtotal = $price * $count;
96
+
97
+ $_SESSION['cart'][$product_id] += array('subtotal' => $subtotal);
98
+
99
+ $total = array_sum(array_column($_SESSION['cart'], 'subtotal'));
100
+
101
+ $total = number_format($total);
102
+
103
+ // $_SESSION['cart'][$product_id] += array('total' => $total);
104
+
105
+ }
106
+
107
+
108
+
109
+ if (empty($_SESSION['cart'])) {
110
+
111
+ $empty_msg = 'カートに商品が入っていません';
112
+
113
+ }
114
+
115
+
116
+
117
+ if (isset($_GET['id'])) {
118
+
119
+ // $delete = $_POST['delete'];
120
+
121
+ unset($_SESSION['cart'][$_GET['id']]);
122
+
123
+ $total = array_sum(array_column($_SESSION['cart'], 'subtotal'));
124
+
125
+ $total = number_format($total);
126
+
127
+ // $_SESSION['cart'][$product_id] += array('total' => $total);
128
+
129
+ }
130
+
131
+
132
+
133
+ $total = array_sum(array_column($_SESSION['cart'], 'subtotal'));
134
+
135
+ $total = number_format($total);
136
+
137
+
138
+
139
+ ?>
140
+
141
+
142
+
143
+ <!DOCTYPE html>
144
+
145
+ <html lang="ja">
146
+
147
+ <head>
148
+
149
+ <meta charset="utf-8">
150
+
151
+ <title>EC SITE</title>
152
+
153
+ <link rel="stylesheet" href="styles.css">
154
+
155
+ </head>
156
+
157
+ <body>
158
+
159
+ <header>
160
+
161
+ <p><?php echo $msg; ?></p>
162
+
163
+ <a href="index.php"><h1>ZAZA TOWN</h1></a>
164
+
165
+ <form action="search.php" method="GET">
166
+
167
+ <input type="search" name="search" placeholder="何かお探しですか?">
168
+
169
+ <input type="submit" name="submit" value="検索">
170
+
171
+ </form>
172
+
173
+ <?php echo $link; ?>
174
+
175
+ <a href="history.php">購入履歴</a>
176
+
177
+ <a href="favorite.php"><img src="images/image2.jpeg" alt="お気に入り" title="お気に入り"></a>
178
+
179
+ <a href="cart.php"><img src="images/image1.jpeg" alt="カート" title="カート"></a>
180
+
181
+ </header>
182
+
183
+ <p1>ZAZASALE 2020 開催中 <span>MAX90%OFF</span></p1><br>
184
+
185
+ <p><?php echo $cartmsg; ?></p>
186
+
187
+ <p><?php echo $empty_msg; ?></p>
188
+
189
+ <form action="purchase.php" method="POST" name="form" class="form">
190
+
191
+ <?php foreach ($_SESSION['cart'] as $product_id): ?>
192
+
193
+ <div class="cart_container">
194
+
195
+ <div class="cart_thumbnail">
196
+
197
+ <img src="images/<?php echo h($product_id['productImage']); ?>" title="画像" id="cart_img" width="auto" height="240">
198
+
199
+ </div>
200
+
201
+ <!-- <div class="cart_detail"><br> -->
202
+
203
+ <div class="cart_content">
204
+
205
+ <p>ブランド : <?php echo h($product_id['brandName']); ?></p>
206
+
207
+ <p>アイテム : <?php echo h($product_id['productName']); ?></p>
208
+
209
+ <p>サイズ  : <?php echo h($product_id['size']); ?></p>
210
+
211
+ <!-- <p>個数   : <?php echo h($product_id['count']); ?></p> -->
212
+
213
+ <p>個数  /
214
+
215
+ <select name="count" id="count" class="count">
216
+
217
+ <?php for ($i=1; $i<=10; $i++) {
218
+
219
+ echo "<option value=" .$i.">".$i."</option>";
220
+
21
- ソースコード
221
+ } ?>
222
+
223
+ </select></p>
224
+
225
+ <p id="id">id   : <?php echo h($product_id['product_id']); ?></p>
226
+
227
+ <p id="price" class="price"><?php echo number_format(h($product_id['price'])); ?><span> (税込)</span></p>
228
+
229
+ <!-- <p id="price">1<span> (税込)</span></p> -->
230
+
231
+ <p><a href="cart.php?id=<?php echo h($product_id['product_id']); ?>" name="delete">削除</a></p>
232
+
233
+ </div>
234
+
235
+ <p id="subtotal" class="subtotal" data>小計 <?php echo number_format(h($product_id['price'])); ?><span> (税込)</span></p>
236
+
237
+ </div>
238
+
239
+ <?php endforeach; ?>
240
+
241
+ <input type="hidden" name="product_id" value="<?php echo h($product_id['product_id']); ?>">
242
+
243
+ <input type="hidden" name="productImage" value="<?php echo h($product_id['productImage']); ?>">
244
+
245
+ <input type="hidden" name="productName" value="<?php echo h($product_id['productName']); ?>">
246
+
247
+ <input type="hidden" name="brandName" value="<?php echo h($product_id['brandName']); ?>">
248
+
249
+ <input type="hidden" name="size" value="<?php echo h($product_id['size']); ?>">
250
+
251
+ <input type="hidden" name="count" value="<?php echo h($product_id['count']); ?>">
252
+
253
+ <input type="hidden" name="subtotal" value="<?php echo h($product_id['subtotal']); ?>">
254
+
255
+ <input type="hidden" name="total" value="<?php echo h($total); ?>">
256
+
257
+
258
+
259
+ <div class="purchase">商品合計 ¥
260
+
261
+ <!-- <p class="total">商品合計   ¥<?php echo h($total); ?><span> (税込)</span></p> -->
262
+
263
+ <p class="total"><?php echo h($total); ?></p><span> (税込)</span>
264
+
265
+ </div>
266
+
267
+
268
+
269
+ <p class="buy">
270
+
271
+ <input type="submit" name="purchase" value="購入手続き">
272
+
273
+ </p>
274
+
275
+ </form>
276
+
277
+ <p><a href="index.php">買い物を続ける</a></p>
278
+
279
+
280
+
281
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
282
+
283
+ <script>
284
+
285
+
286
+
287
+ $(function() {
288
+
289
+ var sessionCart = <?php echo json_encode($_SESSION['cart']); ?>;
290
+
291
+
292
+
293
+ $('select').on('change', function() {
294
+
295
+
296
+
297
+ // $('select').each(function() {
298
+
299
+ var selected = $(this).val();
300
+
301
+
302
+
303
+ console.log(selected);
304
+
305
+
306
+
307
+ Object.values(sessionCart).forEach(function(value, index) {
308
+
309
+ console.log(value.price + '[' + value.product_id + ']');
310
+
311
+ const total = value.price * selected;
312
+
313
+ console.log(total);
314
+
315
+
316
+
317
+ $.ajax({
318
+
319
+ type: "POST",
320
+
321
+ url: "http://192.168.33.10:8000/cart.php",
322
+
323
+    data: {"total": "total"}
324
+
325
+ }).done(function() {
326
+
327
+ // $('#subtotal').each(function() {
328
+
329
+ $('#subtotal').text('小計 ' + total + '(税込)');
330
+
331
+ // });
332
+
333
+ }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
334
+
335
+ alert(errorThrown);
336
+
337
+ });
338
+
339
+ });
340
+
341
+ // });
342
+
343
+ });
344
+
345
+ });
346
+
347
+ </script>
348
+
349
+ </body>
350
+
351
+
352
+
353
+ </html>
354
+
355
+
22
356
 
23
357
  ```
24
358