質問編集履歴

4

追記

2021/03/05 13:14

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -54,22 +54,372 @@
54
54
 
55
55
  ここに選手の得点履歴として、「点数(何点目か)」「試合日時」「対戦相手」「ゴールタイム」を追加したい。
56
56
 
57
- すでに実装ずみのsql文
58
-
59
- ```
60
-
61
- $sql = 'SELECT c.name AS c_name, p.id, p.uniform_num, p.position, p.name, p.club, p.birth, p.height, p.weight
62
-
63
- FROM players p
64
-
65
- INNER JOIN countries c
66
-
67
- ON c.id = p.country_id
68
-
69
- WHERE p.id = :id';
70
-
71
- ```
72
-
73
57
 
74
58
 
75
59
  このsql文で上記画像が実装できている
60
+
61
+ Player.php
62
+
63
+ ```
64
+
65
+ public function findById($id = 0):Array {
66
+
67
+ $sql = 'SELECT c.name AS c_name, p.id, p.uniform_num, p.position, p.name, p.club, p.birth, p.height, p.weight
68
+
69
+ FROM players p
70
+
71
+ INNER JOIN countries c
72
+
73
+ ON c.id = p.country_id
74
+
75
+ WHERE p.id = :id';
76
+
77
+
78
+
79
+ $sql_score = 'SELECT
80
+
81
+ (
82
+
83
+ SELECT
84
+
85
+ COUNT(1) + 1
86
+
87
+ FROM
88
+
89
+ goals g
90
+
91
+ INNER JOIN pairings p
92
+
93
+ ON g.pairing_id = p.id
94
+
95
+ WHERE
96
+
97
+ goals.player_id = g.player_id
98
+
99
+ AND (
100
+
101
+ (
102
+
103
+ goals.goal_time > g.goal_time
104
+
105
+ AND pairings.kickoff = p.kickoff
106
+
107
+ ) OR (
108
+
109
+ pairings.kickoff > p.kickoff
110
+
111
+ )
112
+
113
+ )
114
+
115
+ ) AS 点数
116
+
117
+ , pairings.kickoff AS 試合日時
118
+
119
+ , countries.name AS 対戦相手
120
+
121
+ , goals.goal_time AS ゴールタイム
122
+
123
+ FROM
124
+
125
+ players
126
+
127
+ INNER JOIN goals
128
+
129
+ ON players.id = goals.player_id
130
+
131
+ INNER JOIN pairings
132
+
133
+ ON goals.pairing_id = pairings.id
134
+
135
+ INNER JOIN countries
136
+
137
+ ON pairings.enemy_country_id = countries.id
138
+
139
+ WHERE
140
+
141
+ players.id = :id
142
+
143
+ ORDER BY
144
+
145
+ pairings.kickoff
146
+
147
+ , goals.goal_time';
148
+
149
+
150
+
151
+ $sth = $this->dbh->prepare($sql);
152
+
153
+ $sth_score = $this->dbh->prepare($sql_score);
154
+
155
+ $sth->bindParam(':id', $id, PDO::PARAM_INT);
156
+
157
+ $sth_score->bindParam(':id', $id, PDO::PARAM_INT);
158
+
159
+ $sth->execute();
160
+
161
+ $sth_score->execute();
162
+
163
+ $result = $sth->fetch(PDO::FETCH_ASSOC);
164
+
165
+ $result_score = $sth_score->fetch(PDO::FETCH_ASSOC);
166
+
167
+ return array($result,$result_score);
168
+
169
+ }
170
+
171
+ ```
172
+
173
+ PlayerControler.php
174
+
175
+ ```
176
+
177
+ <?php
178
+
179
+ require_once(ROOT_PATH .'/Models/Player.php');
180
+
181
+ class PlayerController {
182
+
183
+ private $request;
184
+
185
+ private $player;
186
+
187
+ public function __construct() {
188
+
189
+ $this->request['get'] = $_GET;
190
+
191
+ $this->request['post'] = $_POST;
192
+
193
+ $this->Player = new Player();
194
+
195
+ }
196
+
197
+
198
+
199
+ public function index() {
200
+
201
+ $page = 0;
202
+
203
+ if(isset($this->request['get']['page'])) {
204
+
205
+ $page = $this->request['get']['page'];
206
+
207
+ }
208
+
209
+
210
+
211
+ $players = $this->Player->findAll($page);
212
+
213
+ $players_count = $this->Player->countAll();
214
+
215
+ $params = [
216
+
217
+ 'players' => $players,
218
+
219
+ 'pages' => $players_count / 20
220
+
221
+ ];
222
+
223
+ return $params;
224
+
225
+ }
226
+
227
+
228
+
229
+ public function view() {
230
+
231
+ if(empty($this->request['get']['id'])) {
232
+
233
+ echo '指定のパラメーターが不正です。このページを表示できません。';
234
+
235
+ exit;
236
+
237
+ }
238
+
239
+
240
+
241
+ $player = $this->Player->findById($this->request['get']['id']);
242
+
243
+ $params = [
244
+
245
+ 'player' => $player
246
+
247
+ ];
248
+
249
+ return $params;
250
+
251
+ }
252
+
253
+ }
254
+
255
+ ```
256
+
257
+ show.php
258
+
259
+ ```
260
+
261
+ <?php
262
+
263
+ ini_set('display_errors', "On");
264
+
265
+ require_once(ROOT_PATH .'Controllers/PlayerController.php');
266
+
267
+ require_once(ROOT_PATH .'/Models/Player.php');
268
+
269
+ $player = new PlayerController();
270
+
271
+ $params = $player->view();
272
+
273
+ var_dump($params);
274
+
275
+ ?>
276
+
277
+
278
+
279
+ <!DOCTYPE html>
280
+
281
+ <html lang="en">
282
+
283
+ <head>
284
+
285
+ <meta charset="UTF-8">
286
+
287
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
288
+
289
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
290
+
291
+ <link rel="stylesheet" href="/css/base.css">
292
+
293
+ <title>worldcup</title>
294
+
295
+ </head>
296
+
297
+ <body>
298
+
299
+ <h2>選手詳細</h2>
300
+
301
+ <table class="show-table">
302
+
303
+ <?php foreach($params['player'] as $player): ?>
304
+
305
+ <tr>
306
+
307
+ <th>No</th>
308
+
309
+ <td><?=$player['id'] ?></td>
310
+
311
+ </tr>
312
+
313
+ <tr>
314
+
315
+ <th>背番号</th>
316
+
317
+ <td><?=$player['uniform_num'] ?></td>
318
+
319
+ </tr>
320
+
321
+ <tr>
322
+
323
+ <th>ポジション</th>
324
+
325
+ <td><?=$player['position'] ?></td>
326
+
327
+ </tr>
328
+
329
+ <tr>
330
+
331
+ <th>名前</th>
332
+
333
+ <td><?=$player['name'] ?></td>
334
+
335
+ </tr>
336
+
337
+ <tr>
338
+
339
+ <th>所属</th>
340
+
341
+ <td><?=$player['club'] ?></td>
342
+
343
+ </tr>
344
+
345
+ <tr>
346
+
347
+ <th>誕生日</th>
348
+
349
+ <td><?=$player['birth'] ?></td>
350
+
351
+ </tr>
352
+
353
+ <tr>
354
+
355
+ <th>身長</th>
356
+
357
+ <td><?=$player['height'] ?>cm</td>
358
+
359
+ </tr>
360
+
361
+ <tr>
362
+
363
+ <th>体重</th>
364
+
365
+ <td><?=$player['weight'] ?>kg</td>
366
+
367
+ </tr>
368
+
369
+ <tr>
370
+
371
+ <th>所属国</th>
372
+
373
+ <td><?=$player['c_name'] ?></td>
374
+
375
+ </tr>
376
+
377
+ <tr>
378
+
379
+ <th>得点</th>
380
+
381
+ <td><?=$player['点数'] ?></td>
382
+
383
+ </tr>
384
+
385
+ <?php endforeach; ?>
386
+
387
+ </table>
388
+
389
+ <div class="ed-btn">
390
+
391
+ <div class="edit"><a href="show.php?id=<?php echo $player['id'] ?>">編集</a></div>
392
+
393
+ <div class="delete"><a href="show.php?id=<?php echo $player['id'] ?>">削除</a></div>
394
+
395
+ </div>
396
+
397
+ <br>
398
+
399
+ <div class="top">
400
+
401
+ <a href="index.php">トップへ戻る</a>
402
+
403
+ </div>
404
+
405
+ </body>
406
+
407
+ </html>
408
+
409
+
410
+
411
+ ```
412
+
413
+
414
+
415
+ 得点表示
416
+
417
+ ![イメージ説明](45eccc0bca4a1e5ceafeddef3a1d7f44.png)
418
+
419
+ ![イメージ説明](8327a116d6fab401c7844e01ce7ea924.png)
420
+
421
+
422
+
423
+ 選手詳細はうまく表示できるのですが、得点が表示できずに困っています。
424
+
425
+ var_dumpでも得点部分がbool(false)返ってきます。

3

追記

2021/03/05 13:14

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -53,3 +53,23 @@
53
53
  ![イメージ説明](7cc99624c8ce75a610104000d2893fe5.png)
54
54
 
55
55
  ここに選手の得点履歴として、「点数(何点目か)」「試合日時」「対戦相手」「ゴールタイム」を追加したい。
56
+
57
+ すでに実装ずみのsql文
58
+
59
+ ```
60
+
61
+ $sql = 'SELECT c.name AS c_name, p.id, p.uniform_num, p.position, p.name, p.club, p.birth, p.height, p.weight
62
+
63
+ FROM players p
64
+
65
+ INNER JOIN countries c
66
+
67
+ ON c.id = p.country_id
68
+
69
+ WHERE p.id = :id';
70
+
71
+ ```
72
+
73
+
74
+
75
+ このsql文で上記画像が実装できている

2

完成予想図の追記

2021/03/04 14:22

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,11 @@
45
45
  playersテーブル
46
46
 
47
47
  ![イメージ説明](fbc3233a26a394d6b1e45aae5abd7785.png)
48
+
49
+
50
+
51
+ ##完成予想図
52
+
53
+ ![イメージ説明](7cc99624c8ce75a610104000d2893fe5.png)
54
+
55
+ ここに選手の得点履歴として、「点数(何点目か)」「試合日時」「対戦相手」「ゴールタイム」を追加したい。

1

追記

2021/03/04 13:08

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -24,34 +24,24 @@
24
24
 
25
25
  countriesテーブル
26
26
 
27
- ![イメージ説明](180f2f91d66cd8807b5a34c313f0e8cb.png)
27
+ ![イメージ説明](0ced57bebb7211f227a0289c0846adaa.png)
28
-
29
-
30
28
 
31
29
  goalsテーブル
32
30
 
33
- ![イメージ説明](c5dd1b2cb035296790aa6283845bbcde.png)
31
+ ![イメージ説明](100b4fefc3381aca763c9bf2526b59ce.png)
34
-
35
-
36
32
 
37
33
  goals_tmpテーブル
38
34
 
39
- ![イメージ説明](42c8b3a795b623b7326878843c858601.png)
35
+ ![イメージ説明](c9d8404f4d9e66a32bac10f87ea4cb7c.png)
40
-
41
-
42
36
 
43
37
  pairingsテーブル
44
38
 
45
- ![![イメージ説明](68f7f5d7bc77c3c7d728561196606a1f.png)]
39
+ ![イメージ説明](27be86268f71fe7c700b4b080d84c615.png)
46
-
47
-
48
40
 
49
41
  pairings_tmpテーブル
50
42
 
51
- ![イメージ説明](d80a31ae942a71f9b24643e0d0effc45.png)
43
+ ![イメージ説明](fe864b57f90d4d0a63a1552158b82965.png)
52
-
53
-
54
44
 
55
45
  playersテーブル
56
46
 
57
- ![イメージ説明](c02154543b611c813fe2632fc3a066e6.png)
47
+ ![イメージ説明](fbc3233a26a394d6b1e45aae5abd7785.png)