質問編集履歴

6

タイトルを明確にした

2019/02/26 01:18

投稿

Toshinori23
Toshinori23

スコア19

test CHANGED
@@ -1 +1 @@
1
- progate PHP教材で不明な部分がありご教授お願します。
1
+ INPUTタグ POSTメソッド使方について
test CHANGED
File without changes

5

文章内容の変更

2019/02/26 01:17

投稿

Toshinori23
Toshinori23

スコア19

test CHANGED
File without changes
test CHANGED
@@ -14,298 +14,288 @@
14
14
 
15
15
  ```
16
16
 
17
+
18
+
19
+ ```
20
+
21
+
22
+
23
+ ### 該当のソースコード
24
+
25
+ ```menu.php
26
+
27
+ <?php
28
+
29
+ class Menu {
30
+
31
+ private $name;
32
+
33
+ private $price;
34
+
35
+ private $image;
36
+
37
+ private $orderCount = 0;
38
+
39
+
40
+
41
+ public function __construct($name, $price, $image) {
42
+
43
+ $this->name = $name;
44
+
45
+ $this->price = $price;
46
+
47
+ $this->image = $image;
48
+
49
+ }
50
+
51
+
52
+
53
+ public function hello() {
54
+
55
+ echo '私は'.$this->name.'です';
56
+
57
+ }
58
+
59
+
60
+
61
+ public function getName() {
62
+
63
+ return $this->name;
64
+
65
+ }
66
+
67
+
68
+
69
+ public function getImage() {
70
+
71
+ return $this->image;
72
+
73
+ }
74
+
75
+
76
+
77
+ public function getOrderCount() {
78
+
79
+ return $this->orderCount;
80
+
81
+ }
82
+
83
+
84
+
85
+ public function setOrderCount($orderCount) {
86
+
87
+ $this->orderCount = $orderCount;
88
+
89
+ }
90
+
91
+
92
+
93
+ public function getTaxIncludedPrice() {
94
+
95
+ return floor($this->price * 1.08);
96
+
97
+ }
98
+
99
+
100
+
101
+ }
102
+
103
+ ?>
104
+
105
+ ```
106
+
107
+ ```date.php
108
+
109
+ <?php
110
+
111
+ require_once('menu.php');
112
+
113
+
114
+
115
+ $juice = new Menu('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png');
116
+
117
+ $coffee = new Menu('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png');
118
+
119
+ $curry = new Menu('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png');
120
+
121
+ $pasta = new Menu('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png');
122
+
123
+
124
+
125
+ $menus = array($juice, $coffee, $curry, $pasta);
126
+
127
+
128
+
129
+ ?>
130
+
131
+
132
+
133
+ ```
134
+
135
+
136
+
137
+ ```<?php require_once('data.php') ?>
138
+
139
+
140
+
141
+ <!DOCTYPE html>
142
+
143
+ <html>
144
+
145
+ <head>
146
+
147
+ <meta charset="utf-8">
148
+
149
+ <title>Café Progate</title>
150
+
151
+ <link rel="stylesheet" type="text/css" href="stylesheet.css">
152
+
153
+ <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'>
154
+
155
+ </head>
156
+
157
+ <body>
158
+
159
+ <div class="menu-wrapper container">
160
+
161
+ <h1 class="logo">Café Progate</h1>
162
+
163
+ <form method="post" action="confirm.php">
164
+
165
+ <div class="menu-items">
166
+
167
+ <?php foreach ($menus as $menu): ?>
168
+
169
+ <div class="menu-item">
170
+
171
+ <img src="<?php echo $menu->getImage() ?>" class="menu-item-image">
172
+
173
+ <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3>
174
+
175
+ <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p>
176
+
177
+ <input type="text" value="0" name="<?php echo $menu->getName() ?>">
178
+
179
+ <span>個</span>
180
+
181
+ </div>
182
+
183
+ <?php endforeach ?>
184
+
185
+ </div>
186
+
187
+ <input type="submit" value="注文する">
188
+
189
+ </form>
190
+
191
+ </div>
192
+
193
+ </body>
194
+
195
+ </html>
196
+
197
+
198
+
199
+ ```
200
+
201
+
202
+
203
+
204
+
205
+ ```confirm.php
206
+
207
+ <?php require_once('data.php') ?>
208
+
209
+
210
+
211
+ <!DOCTYPE html>
212
+
213
+ <html>
214
+
215
+ <head>
216
+
217
+ <meta charset="utf-8">
218
+
219
+ <title>Progate</title>
220
+
221
+ <link rel="stylesheet" type="text/css" href="stylesheet.css">
222
+
223
+ <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'>
224
+
225
+ </head>
226
+
227
+ <body>
228
+
229
+ <div class="order-wrapper">
230
+
231
+ <h2>注文内容確認</h2>
232
+
233
+ <?php foreach ($menus as $menu): ?>
234
+
235
+ <!-- 変数$orderCountに$_POSTで受け取った値を代入してください -->
236
+
237
+ <?php $orderCount = $_POST[$menu->getName()] ?>
238
+
239
+ <p class="order-amount">
240
+
241
+ <!-- ここに、$menuのゲッターを用いてnameプロパティを表示してください -->
242
+
243
+ <?php echo $menu->getName() ?>
244
+
245
+ x
246
+
247
+ <!-- ここに、$orderCountを表示してください -->
248
+
249
+ <?php echo $orderCount ?>
250
+
251
+
252
+
253
+ </p>
254
+
255
+ <?php endforeach ?>
256
+
257
+ </div>
258
+
259
+ </body>
260
+
261
+ </html>
262
+
263
+ ```
264
+
265
+
266
+
267
+ ### 試したこと
268
+
269
+
270
+
271
+ また上のソースコードにはないのですが、試しにMenuクラスで商品の数private $num=0を定義し、
272
+
273
+
274
+
275
+ public function getNum(){return $this->num}
276
+
277
+
278
+
279
+ を作り、
280
+
281
+
282
+
283
+ <input type="text" value="0" name="<?php echo $menu->getName() ?>">
284
+
285
+
286
+
287
+ <?php $orderCount=$_POST[$menu->getName()]?>
288
+
289
+
290
+
291
+ とし、$menu->$getNum()を$menu->$getNameに代入したところ、数値が0のまま変わりませんでした。![イメージ説明](c60e6aeb673787a73073cc02d3864b5f.png)
292
+
293
+ ![イメージ説明](69cffb4c9cd31613fb5edea656195a51.png)
294
+
295
+
296
+
17
297
  $menu->getNumの数値(例:2)が反映されません。おそらくクラス部分のgetNum()メソッドに何か不備ががあるのですが、それを発見する方法が分かりません。何かアドバイスや修正できる点など教えていただけましたら幸いです。
18
298
 
19
- ```
20
-
21
-
22
-
23
- ### 該当のソースコード
24
-
25
- ```menu.php
26
-
27
- <?php
28
-
29
- class Menu {
30
-
31
- private $name;
32
-
33
- private $num = 0;
34
-
35
- private $price;
36
-
37
- private $image;
38
-
39
- private $orderCount = 0;
40
-
41
-
42
-
43
- public function __construct($name, $price, $image) {
44
-
45
- $this->name = $name;
46
-
47
- $this->price = $price;
48
-
49
- $this->image = $image;
50
-
51
- }
52
-
53
-
54
-
55
- public function hello() {
56
-
57
- echo '私は'.$this->name.'です';
58
-
59
- }
60
-
61
-
62
-
63
- public function getName() {
64
-
65
- return $this->name;
66
-
67
- }
68
-
69
-
70
-
71
- public function getImage() {
72
-
73
- return $this->image;
74
-
75
- }
76
-
77
-
78
-
79
- public function getOrderCount() {
80
-
81
- return $this->orderCount;
82
-
83
- }
84
-
85
-
86
-
87
- public function setOrderCount($orderCount) {
88
-
89
- $this->orderCount = $orderCount;
90
-
91
- }
92
-
93
-
94
-
95
- public function getTaxIncludedPrice() {
96
-
97
- return floor($this->price * 1.08);
98
-
99
- }
100
-
101
-
102
-
103
- public function getNum(){
104
-
105
- return $this->num;
106
-
107
- }
108
-
109
-
110
-
111
- }
112
-
113
- ?>
114
-
115
- ```
116
-
117
- ```date.php
118
-
119
- <?php
120
-
121
- require_once('menu.php');
122
-
123
-
124
-
125
- $juice = new Menu('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png');
126
-
127
- $coffee = new Menu('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png');
128
-
129
- $curry = new Menu('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png');
130
-
131
- $pasta = new Menu('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png');
132
-
133
-
134
-
135
- $menus = array($juice, $coffee, $curry, $pasta);
136
-
137
-
138
-
139
- ?>
140
-
141
-
142
-
143
- ```
144
-
145
- ```index.php
146
-
147
- <?php require_once('data.php') ?>
148
-
149
-
150
-
151
- <!DOCTYPE html>
152
-
153
- <html>
154
-
155
- <head>
156
-
157
- <meta charset="utf-8">
158
-
159
- <title>Café Progate</title>
160
-
161
- <link rel="stylesheet" type="text/css" href="stylesheet.css">
162
-
163
- <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'>
164
-
165
- </head>
166
-
167
- <body>
168
-
169
- <div class="menu-wrapper container">
170
-
171
- <h1 class="logo">Café Progate</h1>
172
-
173
- <form method="post" action="confirm.php">
174
-
175
- <div class="menu-items">
176
-
177
- <?php foreach ($menus as $menu): ?>
178
-
179
- <div class="menu-item">
180
-
181
- <img src="<?php echo $menu->getImage() ?>" class="menu-item-image">
182
-
183
- <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3>
184
-
185
- <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p>
186
-
187
- <input type="text" value="0" name="<?php echo $menu->getNum() ?>">
188
-
189
- <span>個</span>
190
-
191
- </div>
192
-
193
- <?php endforeach ?>
194
-
195
- </div>
196
-
197
- <input type="submit" value="注文する">
198
-
199
- </form>
200
-
201
- </div>
202
-
203
- </body>
204
-
205
- </html>
206
-
207
-
208
-
209
- ```
210
-
211
-
212
-
213
-
214
-
215
- ```confirm.php
216
-
217
- <?php require_once('data.php') ?>
218
-
219
-
220
-
221
- <!DOCTYPE html>
222
-
223
- <html>
224
-
225
- <head>
226
-
227
- <meta charset="utf-8">
228
-
229
- <title>Progate</title>
230
-
231
- <link rel="stylesheet" type="text/css" href="stylesheet.css">
232
-
233
- <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'>
234
-
235
- </head>
236
-
237
- <body>
238
-
239
- <div class="order-wrapper">
240
-
241
- <h2>注文内容確認</h2>
242
-
243
- <?php foreach ($menus as $menu): ?>
244
-
245
- <!-- 変数$orderCountに$_POSTで受け取った値を代入してください -->
246
-
247
- <?php $orderCount=$_POST[$menu->getNum()]?>
248
-
249
- <p class="order-amount">
250
-
251
- <!-- ここに、$menuのゲッターを用いてnameプロパティを表示してください -->
252
-
253
- <?php echo $menu->getName() ?>
254
-
255
- x
256
-
257
- <!-- ここに、$orderCountを表示してください -->
258
-
259
- <?php echo $orderCount ?>
260
-
261
-
262
-
263
- </p>
264
-
265
- <?php endforeach ?>
266
-
267
- </div>
268
-
269
- </body>
270
-
271
- </html>
272
-
273
- ```
274
-
275
-
276
-
277
- ### 試したこと
278
-
279
-
280
-
281
- 試しにMenuクラスで商品の数private $num=0を定義し、
282
-
283
-
284
-
285
- public function getNum(){return $this->num}
286
-
287
-
288
-
289
- を作り、
290
-
291
-
292
-
293
- <input type="text" value="0" name="<?php echo $menu->getName() ?>">
294
-
295
-
296
-
297
- <?php $orderCount=$_POST[$menu->getName()]?>
298
-
299
-
300
-
301
- としたところ、$menu->$getNum()を$menu->$getNameに代入したところ、数値が0のまま変わりませんでした。![イメージ説明](c60e6aeb673787a73073cc02d3864b5f.png)
302
-
303
- ![イメージ説明](69cffb4c9cd31613fb5edea656195a51.png)
304
-
305
-
306
-
307
- 右側画面は上のソースの一部です。
308
-
309
299
 
310
300
 
311
301
  ### 補足情報(FW/ツールのバージョンなど)

4

書式の改善

2019/02/25 14:53

投稿

Toshinori23
Toshinori23

スコア19

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  ```
16
16
 
17
- $menu->getNameは商品名前(例:'CURRY')であるはずなのに、教材では$menu->getName()に数値(例:2)を入ていしたなぜ$menu->getName()商品名は商品名を表すものなのも関わらず、数値ことができるしょうか?
17
+ $menu->getNumの数値(例:2)が反映されませんおそらくクラス部分のgetNum()メソッド何か不備があのです、それを発見する方法が分かりません。何かアドバイスや修正できる点など教えていただけましたら幸いす。
18
18
 
19
19
  ```
20
20
 

3

見難かったので修正しました

2019/02/25 13:55

投稿

Toshinori23
Toshinori23

スコア19

test CHANGED
File without changes
test CHANGED
@@ -30,6 +30,8 @@
30
30
 
31
31
  private $name;
32
32
 
33
+ private $num = 0;
34
+
33
35
  private $price;
34
36
 
35
37
  private $image;
@@ -98,6 +100,14 @@
98
100
 
99
101
 
100
102
 
103
+ public function getNum(){
104
+
105
+ return $this->num;
106
+
107
+ }
108
+
109
+
110
+
101
111
  }
102
112
 
103
113
  ?>
@@ -128,6 +138,8 @@
128
138
 
129
139
  ?>
130
140
 
141
+
142
+
131
143
  ```
132
144
 
133
145
  ```index.php
@@ -172,11 +184,7 @@
172
184
 
173
185
  <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p>
174
186
 
175
-
176
-
177
- <input type="text" value="0" name="<?php echo $menu->getName() ?>">
187
+ <input type="text" value="0" name="<?php echo $menu->getNum() ?>">
178
-
179
-
180
188
 
181
189
  <span>個</span>
182
190
 
@@ -200,6 +208,10 @@
200
208
 
201
209
  ```
202
210
 
211
+
212
+
213
+
214
+
203
215
  ```confirm.php
204
216
 
205
217
  <?php require_once('data.php') ?>
@@ -232,7 +244,7 @@
232
244
 
233
245
  <!-- 変数$orderCountに$_POSTで受け取った値を代入してください -->
234
246
 
235
- <?php $orderCount=$_POST[$menu->getName()]?>
247
+ <?php $orderCount=$_POST[$menu->getNum()]?>
236
248
 
237
249
  <p class="order-amount">
238
250
 

2

画像などの追加

2019/02/25 13:45

投稿

Toshinori23
Toshinori23

スコア19

test CHANGED
File without changes
test CHANGED
@@ -266,7 +266,7 @@
266
266
 
267
267
 
268
268
 
269
- 試しにMenuクラスで商品の数private $numを定義し、
269
+ 試しにMenuクラスで商品の数private $num=0を定義し、
270
270
 
271
271
 
272
272
 
@@ -278,15 +278,21 @@
278
278
 
279
279
 
280
280
 
281
- <input type="text" value="0" name="<?php echo $menu->getNum() ?>">
281
+ <input type="text" value="0" name="<?php echo $menu->getName() ?>">
282
-
283
-
284
-
282
+
283
+
284
+
285
- <?php $orderCount=$_POST[$menu->getNum()]?>
285
+ <?php $orderCount=$_POST[$menu->getName()]?>
286
+
287
+
288
+
286
-
289
+ としたところ、$menu->$getNum()を$menu->$getNameに代入したところ、数値が0のまま変わりませんでした。![イメージ説明](c60e6aeb673787a73073cc02d3864b5f.png)
290
+
287
-
291
+ ![イメージ説明](69cffb4c9cd31613fb5edea656195a51.png)
288
-
292
+
293
+
294
+
289
- としたところエラになってしまいました
295
+ 右側画面は上のソスの一部です。
290
296
 
291
297
 
292
298
 

1

見難かったので修正しました

2019/02/25 13:34

投稿

Toshinori23
Toshinori23

スコア19

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  ```
16
16
 
17
- $menu->getNameは商品の名前(例:'CURRY')であるはずなのに、教材では$menu->getName()に数値(例:2)を入れていました。なぜ$menu->getName()商品名は商品名を表すものなのにも関わらず、数値が入ることができるのでしょうか?関連がありそうなところを太文字で表示しました。
17
+ $menu->getNameは商品の名前(例:'CURRY')であるはずなのに、教材では$menu->getName()に数値(例:2)を入れていました。なぜ$menu->getName()商品名は商品名を表すものなのにも関わらず、数値が入ることができるのでしょうか?
18
18
 
19
19
  ```
20
20
 
@@ -22,13 +22,9 @@
22
22
 
23
23
  ### 該当のソースコード
24
24
 
25
-
26
-
27
- PHP```ここに言語名を入力 
25
+ ```menu.php
28
-
29
- ソースコード
26
+
30
-
31
- ```<?php
27
+ <?php
32
28
 
33
29
  class Menu {
34
30
 
@@ -62,11 +58,11 @@
62
58
 
63
59
 
64
60
 
65
- **public function getName() {
61
+ public function getName() {
66
62
 
67
63
  return $this->name;
68
64
 
69
- }**
65
+ }
70
66
 
71
67
 
72
68
 
@@ -106,7 +102,9 @@
106
102
 
107
103
  ?>
108
104
 
109
-
105
+ ```
106
+
107
+ ```date.php
110
108
 
111
109
  <?php
112
110
 
@@ -114,7 +112,7 @@
114
112
 
115
113
 
116
114
 
117
- $juice = new Menu('JUICE', 600, 'https://s3-ap-northeast- 1.amazonaws.com/progate/shared/images/lesson/php/juice.png');
115
+ $juice = new Menu('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png');
118
116
 
119
117
  $coffee = new Menu('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png');
120
118
 
@@ -130,7 +128,9 @@
130
128
 
131
129
  ?>
132
130
 
133
-
131
+ ```
132
+
133
+ ```index.php
134
134
 
135
135
  <?php require_once('data.php') ?>
136
136
 
@@ -172,7 +172,11 @@
172
172
 
173
173
  <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p>
174
174
 
175
+
176
+
175
- **<input type="text" value="0" name="<?php echo $menu->getName() ?>">**
177
+ <input type="text" value="0" name="<?php echo $menu->getName() ?>">
178
+
179
+
176
180
 
177
181
  <span>個</span>
178
182
 
@@ -194,6 +198,10 @@
194
198
 
195
199
 
196
200
 
201
+ ```
202
+
203
+ ```confirm.php
204
+
197
205
  <?php require_once('data.php') ?>
198
206
 
199
207
 
@@ -222,14 +230,20 @@
222
230
 
223
231
  <?php foreach ($menus as $menu): ?>
224
232
 
233
+ <!-- 変数$orderCountに$_POSTで受け取った値を代入してください -->
234
+
225
- **<?php $orderCount=$_POST[$menu->getName()]?>**
235
+ <?php $orderCount=$_POST[$menu->getName()]?>
226
236
 
227
237
  <p class="order-amount">
228
238
 
239
+ <!-- ここに、$menuのゲッターを用いてnameプロパティを表示してください -->
240
+
229
241
  <?php echo $menu->getName() ?>
230
242
 
231
243
  x
232
244
 
245
+ <!-- ここに、$orderCountを表示してください -->
246
+
233
247
  <?php echo $orderCount ?>
234
248
 
235
249
 
@@ -244,6 +258,10 @@
244
258
 
245
259
  </html>
246
260
 
261
+ ```
262
+
263
+
264
+
247
265
  ### 試したこと
248
266
 
249
267