質問編集履歴
1
問題のコードとDBのデータをを全て掲載しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,47 +8,589 @@
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
|
+
商品一覧ページ(product.php)
|
12
|
+
|
11
|
-
```
|
13
|
+
```php
|
14
|
+
|
12
|
-
|
15
|
+
<!DOCTYPE html>
|
16
|
+
|
17
|
+
<html lang="ja">
|
18
|
+
|
19
|
+
<?php
|
20
|
+
|
21
|
+
$siteTitle = '商品ページ';
|
22
|
+
|
23
|
+
?>
|
24
|
+
|
25
|
+
<head>
|
26
|
+
|
27
|
+
<meta charset="utf-8">
|
28
|
+
|
29
|
+
<title>サイトタイトル</title>
|
30
|
+
|
31
|
+
<meta name="description" content="ディスクリプションを入力">
|
32
|
+
|
33
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
34
|
+
|
35
|
+
<link rel="stylesheet" href="style.css">
|
36
|
+
|
37
|
+
<!-- [if lt IE 9] -->
|
38
|
+
|
39
|
+
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
<!-- [endif] -->
|
44
|
+
|
45
|
+
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
|
46
|
+
|
47
|
+
<script src="app.js"></script>
|
48
|
+
|
49
|
+
<script src="fav.js"></script>
|
50
|
+
|
51
|
+
</head>
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
<body>
|
56
|
+
|
57
|
+
<!----- header----->
|
58
|
+
|
59
|
+
<header>
|
60
|
+
|
61
|
+
<div class="logo">
|
62
|
+
|
63
|
+
<a href="top.php">極楽<br>酒造</a>
|
64
|
+
|
65
|
+
</div>
|
66
|
+
|
67
|
+
<h1>
|
68
|
+
|
69
|
+
<?php echo $siteTitle; ?>
|
70
|
+
|
71
|
+
</h1>
|
72
|
+
|
73
|
+
<div class="cart-box">カートを見る(<span id="cart-count">0</span>)</div>
|
74
|
+
|
75
|
+
<?php
|
76
|
+
|
77
|
+
require('function.php');
|
78
|
+
|
79
|
+
$dbh = db_connect();
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
$stmt = $dbh->prepare("SELECT id, category_name from category where big_flg=1 order by id");
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
$stmt->execute();
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
?>
|
98
|
+
|
99
|
+
<ul class="drop">
|
100
|
+
|
101
|
+
<li><a href="top.php">HOME</a></li>
|
102
|
+
|
103
|
+
<?php
|
104
|
+
|
105
|
+
foreach($results as $rec){
|
106
|
+
|
107
|
+
?>
|
108
|
+
|
109
|
+
<li><?php echo $rec['category_name'];
|
110
|
+
|
111
|
+
?>
|
112
|
+
|
113
|
+
<ul class="drop_menu">
|
114
|
+
|
115
|
+
<?php
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
$sql = "SELECT id, category_name, big_id from category where big_flg=0 and big_id = :big_id order by id";
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
$params = array(':big_id' => $rec['id']);
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
$stmt2 = sql_exec($dbh, $sql, $params);
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
$results2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
foreach($results2 as $rec2){
|
136
|
+
|
137
|
+
?>
|
138
|
+
|
139
|
+
<li><a href="product.php?id=<?php echo $rec2['id']; ?>"><?php echo $rec2['category_name'];?></a></li>
|
140
|
+
|
141
|
+
<?php
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
?>
|
146
|
+
|
147
|
+
</ul>
|
148
|
+
|
149
|
+
</li>
|
150
|
+
|
151
|
+
<?php
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
?>
|
156
|
+
|
157
|
+
</ul>
|
158
|
+
|
159
|
+
</header>
|
160
|
+
|
161
|
+
<!----- /header ----->
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
<script>
|
166
|
+
|
167
|
+
function checked_inspect(product_id) { //その商品がお気に入り登録されているかどうか確認する関数
|
168
|
+
|
169
|
+
fav = $.cookie("fav_item") ? JSON.parse($.cookie("fav_item")) : [];
|
170
|
+
|
171
|
+
console.log(fav);
|
172
|
+
|
173
|
+
console.log(product_id);
|
174
|
+
|
13
|
-
if (String(fav).indexOf(String(product_id)) > -1) {
|
175
|
+
if (String(fav).indexOf(String(product_id)) > -1) {
|
14
|
-
|
176
|
+
|
15
|
-
$('#sumi_'+product_id).text('登録');
|
177
|
+
$('#sumi_' + product_id).text('登録');
|
16
178
|
|
17
179
|
console.log('含まれている');
|
18
180
|
|
19
|
-
console.log('#sumi_'+product_id);
|
181
|
+
console.log('#sumi_' + product_id);
|
20
182
|
|
21
183
|
} else {
|
22
184
|
|
23
|
-
$('#sumi_'+product_id).text('済');
|
185
|
+
$('#sumi_' + product_id).text('済');
|
24
186
|
|
25
187
|
console.log('含まれていない');
|
26
188
|
|
27
|
-
console.log('#sumi_'+product_id);
|
189
|
+
console.log('#sumi_' + product_id);
|
28
190
|
|
29
191
|
}
|
30
192
|
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
</script>
|
198
|
+
|
199
|
+
<?php
|
200
|
+
|
201
|
+
$siteTitle = '商品ページ';
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
$category_id = $_GET['id'];
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
$sql2 = "SELECT id, product_name, img_pass,product_price from product where category_id = :category_id order by id";
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
$params = array(':category_id' => $category_id);
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
$stmt3 = sql_exec($dbh, $sql2, $params);
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
$results3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
$number = 1;
|
226
|
+
|
227
|
+
?>
|
228
|
+
|
229
|
+
<!----- main ----->
|
230
|
+
|
231
|
+
<div class="main">
|
232
|
+
|
233
|
+
<table class="product-corner">
|
234
|
+
|
235
|
+
<?php
|
236
|
+
|
237
|
+
if(count($results3)>0){
|
238
|
+
|
239
|
+
foreach($results3 as $rec3){
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
?>
|
244
|
+
|
245
|
+
<script>
|
246
|
+
|
247
|
+
var product_id = <?php echo $rec3['id']; ?>;
|
248
|
+
|
249
|
+
checked_inspect(product_id);
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
</script>
|
254
|
+
|
255
|
+
<?php
|
256
|
+
|
257
|
+
if($number%3 == 1){
|
258
|
+
|
259
|
+
?>
|
260
|
+
|
261
|
+
<tr class="product-box">
|
262
|
+
|
263
|
+
<?php
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
?>
|
268
|
+
|
269
|
+
<td class="product-item">
|
270
|
+
|
271
|
+
<img src="<?php echo $rec3['img_pass']; ?>" alt="" class="product-img">
|
272
|
+
|
273
|
+
<span class="product-name">
|
274
|
+
|
275
|
+
<?php echo $rec3['product_name'];
|
276
|
+
|
277
|
+
?>
|
278
|
+
|
279
|
+
</span>
|
280
|
+
|
281
|
+
<span class="product-price">
|
282
|
+
|
283
|
+
<?php echo '¥'.$rec3['product_price'].'(税込)';
|
284
|
+
|
285
|
+
?>
|
286
|
+
|
287
|
+
</span>
|
288
|
+
|
289
|
+
<div class="info-box">
|
290
|
+
|
291
|
+
<div id="<?php echo $rec3['id']; ?>" class="fav_button"><span class="__icon"></span><span class="__text">お気に入り<p id="sumi_<?php echo $rec3['id']; ?>"></p></span>
|
292
|
+
|
293
|
+
</div>
|
294
|
+
|
295
|
+
<button class="cart-in">
|
296
|
+
|
297
|
+
<select name="cnt">
|
298
|
+
|
299
|
+
<option value="1" selected>1</option>
|
300
|
+
|
301
|
+
<option value="2">2</option>
|
302
|
+
|
303
|
+
<option value="3">3</option>
|
304
|
+
|
305
|
+
<option value="4">4</option>
|
306
|
+
|
307
|
+
<option value="5">5</option>
|
308
|
+
|
309
|
+
</select>
|
310
|
+
|
311
|
+
カートに入れる
|
312
|
+
|
313
|
+
</button>
|
314
|
+
|
315
|
+
</div>
|
316
|
+
|
317
|
+
</td>
|
318
|
+
|
319
|
+
<?php
|
320
|
+
|
321
|
+
if($number/3 == 0 || count($results3) == $number){
|
322
|
+
|
323
|
+
?>
|
324
|
+
|
325
|
+
</tr>
|
326
|
+
|
327
|
+
<?php
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
$number = $number + 1;
|
332
|
+
|
333
|
+
?>
|
334
|
+
|
335
|
+
<?php
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
}else{
|
340
|
+
|
341
|
+
echo '発売中の商品はありません。';
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
?>
|
346
|
+
|
347
|
+
</table>
|
348
|
+
|
349
|
+
</div>
|
350
|
+
|
351
|
+
<!----- /main ----->
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
<!----- footer ----->
|
356
|
+
|
357
|
+
<footer>©︎gokurakusyuzou 2001-2021</footer>
|
358
|
+
|
359
|
+
<!----- /footer ----->
|
360
|
+
|
361
|
+
</body>
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
</html>
|
366
|
+
|
367
|
+
|
368
|
+
|
31
369
|
```
|
32
370
|
|
33
371
|
|
34
372
|
|
373
|
+
設定関係の処理をまとめたfunction.php
|
374
|
+
|
35
|
-
```h
|
375
|
+
```php
|
376
|
+
|
36
|
-
|
377
|
+
<?php
|
378
|
+
|
379
|
+
|
380
|
+
|
37
|
-
|
381
|
+
ini_set("log_errors", "On");
|
382
|
+
|
38
|
-
|
383
|
+
ini_set("error_log", "error.log");
|
384
|
+
|
385
|
+
|
386
|
+
|
39
|
-
|
387
|
+
session_save_path("/var/tmp");
|
40
|
-
|
388
|
+
|
41
|
-
|
389
|
+
ini_set('session.gc_maxlifetime',60*60*24*30);
|
42
|
-
|
390
|
+
|
43
|
-
|
391
|
+
ini_set('session.cookie_lifetime',60*60*24*30);
|
392
|
+
|
44
|
-
|
393
|
+
session_start();
|
394
|
+
|
395
|
+
session_regenerate_id();
|
396
|
+
|
397
|
+
|
398
|
+
|
45
|
-
|
399
|
+
// デバッグフラグ
|
400
|
+
|
401
|
+
$debug_flg = true;
|
402
|
+
|
403
|
+
// デバッグログ関数
|
404
|
+
|
405
|
+
function debug($str){
|
406
|
+
|
407
|
+
global $debug_flg;
|
408
|
+
|
409
|
+
if(!empty($debug_flg)){
|
410
|
+
|
411
|
+
error_log('デバッグ:'.$str);
|
412
|
+
|
413
|
+
}
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
define('DB_USERNAME', 'root');
|
420
|
+
|
421
|
+
define('DB_PASSWORD', 'root');
|
422
|
+
|
423
|
+
define('DSN', 'mysql:host=localhost; dbname=paradise; charset=utf8');
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
function db_connect(){
|
430
|
+
|
431
|
+
$dbh = new PDO(DSN, DB_USERNAME, DB_PASSWORD);
|
432
|
+
|
433
|
+
return $dbh;
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
function sql_exec($dbh,$query,$params){
|
440
|
+
|
441
|
+
$stmt = $dbh->prepare($query);
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
if(!$stmt->execute($params)){
|
446
|
+
|
447
|
+
debug('SQL実行に失敗しました');
|
448
|
+
|
449
|
+
debug('失敗したSQL:'.print_r($stmt,true));
|
450
|
+
|
451
|
+
return 0;
|
452
|
+
|
453
|
+
}
|
454
|
+
|
455
|
+
debug('SQL成功');
|
456
|
+
|
457
|
+
debug('成功したSQL:'.print_r($stmt,true));
|
458
|
+
|
459
|
+
return $stmt;
|
460
|
+
|
461
|
+
}
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
?>
|
46
466
|
|
47
467
|
```
|
48
468
|
|
49
469
|
|
50
470
|
|
471
|
+
ドロップダウンメニューと商品のデータ
|
472
|
+
|
473
|
+
```sql
|
474
|
+
|
475
|
+
-- テーブルの構造 `category`
|
476
|
+
|
477
|
+
--
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
CREATE TABLE `category` (
|
482
|
+
|
483
|
+
`id` int(11) NOT NULL,
|
484
|
+
|
485
|
+
`category_name` varchar(255) NOT NULL,
|
486
|
+
|
487
|
+
`big_flg` tinyint(1) NOT NULL DEFAULT '0',
|
488
|
+
|
489
|
+
`big_id` int(11) NOT NULL
|
490
|
+
|
491
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
--
|
496
|
+
|
497
|
+
-- テーブルのデータのダンプ `category`
|
498
|
+
|
499
|
+
--
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
INSERT INTO `category` (`id`, `category_name`, `big_flg`, `big_id`) VALUES
|
504
|
+
|
505
|
+
(1, '日本酒', 1, 0),
|
506
|
+
|
507
|
+
(2, 'ビール', 1, 0),
|
508
|
+
|
509
|
+
(3, 'おつまみ', 1, 0),
|
510
|
+
|
511
|
+
(4, 'コーヒー', 1, 0),
|
512
|
+
|
513
|
+
(5, '純米酒', 0, 1),
|
514
|
+
|
515
|
+
(6, '果実酒', 0, 1),
|
516
|
+
|
517
|
+
(7, '日本酒ギフト', 0, 1),
|
518
|
+
|
519
|
+
(9, '瓶ビール', 0, 2),
|
520
|
+
|
521
|
+
(10, '缶ビール', 0, 2),
|
522
|
+
|
523
|
+
(11, 'ビールギフト', 0, 2),
|
524
|
+
|
525
|
+
(12, 'エイヒレ', 0, 3),
|
526
|
+
|
527
|
+
(13, 'ウインナー', 0, 3),
|
528
|
+
|
529
|
+
(14, '奈良漬け', 0, 3),
|
530
|
+
|
531
|
+
(15, 'ボトルコーヒー', 0, 4),
|
532
|
+
|
533
|
+
(16, 'コーヒーギフト', 0, 4);
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
-- --------------------------------------------------------
|
538
|
+
|
539
|
+
|
540
|
+
|
541
|
+
--
|
542
|
+
|
543
|
+
-- テーブルの構造 `product`
|
544
|
+
|
545
|
+
--
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
CREATE TABLE `product` (
|
550
|
+
|
551
|
+
`id` int(11) NOT NULL,
|
552
|
+
|
553
|
+
`product_name` varchar(255) NOT NULL,
|
554
|
+
|
555
|
+
`category_id` int(11) NOT NULL,
|
556
|
+
|
557
|
+
`img_pass` varchar(255) NOT NULL,
|
558
|
+
|
559
|
+
`product_price` int(11) NOT NULL
|
560
|
+
|
561
|
+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
562
|
+
|
563
|
+
|
564
|
+
|
565
|
+
--
|
566
|
+
|
567
|
+
-- テーブルのデータのダンプ `product`
|
568
|
+
|
569
|
+
--
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
INSERT INTO `product` (`id`, `product_name`, `category_id`, `img_pass`, `product_price`) VALUES
|
574
|
+
|
575
|
+
(1, '真野鶴', 5, 'http://localhost:8888/js_advance2/img/IMG_2870.jpeg', 500),
|
576
|
+
|
577
|
+
(2, '奈良漬け詰め合わせ(牛蒡と瓜)', 14, 'http://localhost:8888/js_advance2/img/IMG_4652.jpeg', 700),
|
578
|
+
|
579
|
+
(3, '福千歳', 7, 'http://localhost:8888/js_advance2/img/IMG_2906.jpeg', 600),
|
580
|
+
|
581
|
+
(4, '奈良漬詰め合わせ(西瓜ときゅうり)', 14, 'http://localhost:8888/js_advance2/img/IMG_4651.jpeg', 700),
|
582
|
+
|
583
|
+
(5, '櫻正宗', 5, 'http://localhost:8888/js_advance2/img/IMG_2946.jpeg', 500),
|
584
|
+
|
585
|
+
(6, '金亀漬', 14, 'http://localhost:8888/js_advance2/img/IMG_2812.jpeg', 500),
|
586
|
+
|
587
|
+
(7, '賀茂茄子の奈良漬け', 14, 'http://localhost:8888/js_advance2/img/IMG_6866.jpeg', 400);
|
588
|
+
|
589
|
+
```
|
590
|
+
|
591
|
+
|
592
|
+
|
51
|
-
console.logの内容が出力されていることからこのコードに到達していること
|
593
|
+
checked_inspectメソッド内のconsole.logの内容が出力されていることからこのコードに到達していること
|
52
594
|
|
53
595
|
console.logで出力したpタグのidでhtml部分を検索し、
|
54
596
|
|