質問編集履歴

3

menu.phpの追加

2019/08/25 05:23

投稿

myksgo
myksgo

スコア21

test CHANGED
File without changes
test CHANGED
@@ -210,6 +210,110 @@
210
210
 
211
211
  ```
212
212
 
213
+ ```php
214
+
215
+ //menu.php
216
+
217
+ <?php
218
+
219
+ class Menu {
220
+
221
+ protected $name;
222
+
223
+ protected $price;
224
+
225
+ protected $image;
226
+
227
+ private $orderCount = 0;
228
+
229
+ protected static $count = 0;
230
+
231
+
232
+
233
+ public function __construct($name, $price, $image) {
234
+
235
+ $this->name = $name;
236
+
237
+ $this->price = $price;
238
+
239
+ $this->image = $image;
240
+
241
+ self::$count++;
242
+
243
+ }
244
+
245
+
246
+
247
+ public function hello() {
248
+
249
+ echo '私は'.$this->name.'です';
250
+
251
+ }
252
+
253
+
254
+
255
+ public function getName() {
256
+
257
+ return $this->name;
258
+
259
+ }
260
+
261
+
262
+
263
+ public function getImage() {
264
+
265
+ return $this->image;
266
+
267
+ }
268
+
269
+
270
+
271
+ public function getOrderCount() {
272
+
273
+ return $this->orderCount;
274
+
275
+ }
276
+
277
+
278
+
279
+ public function setOrderCount($orderCount) {
280
+
281
+ $this->orderCount = $orderCount;
282
+
283
+ }
284
+
285
+
286
+
287
+ public function getTaxIncludedPrice() {
288
+
289
+ return floor($this->price * 1.08);
290
+
291
+ }
292
+
293
+
294
+
295
+ public function getTotalPrice() {
296
+
297
+ return $this->getTaxIncludedPrice() * $this->orderCount;
298
+
299
+ }
300
+
301
+
302
+
303
+ public static function getCount() {
304
+
305
+ return self::$count;
306
+
307
+ }
308
+
309
+
310
+
311
+ }
312
+
313
+ ?>
314
+
315
+ ```
316
+
213
317
  setとgetについてよく分からなくなってしまいましたので、ご教授よろしくお願いいたします。
214
318
 
215
319
 

2

data.phpの追加

2019/08/25 05:23

投稿

myksgo
myksgo

スコア21

test CHANGED
File without changes
test CHANGED
@@ -180,6 +180,36 @@
180
180
 
181
181
  ```
182
182
 
183
+ ```php
184
+
185
+ //data.php
186
+
187
+ <?php
188
+
189
+ require_once('drink.php');
190
+
191
+ require_once('food.php');
192
+
193
+
194
+
195
+ $juice = new Drink('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png', 'アイス');
196
+
197
+ $coffee = new Drink('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png', 'ホット');
198
+
199
+ $curry = new Food('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png', 3);
200
+
201
+ $pasta = new Food('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png', 1);
202
+
203
+
204
+
205
+ $menus = array($juice, $coffee, $curry, $pasta);
206
+
207
+
208
+
209
+ ?>
210
+
211
+ ```
212
+
183
213
  setとgetについてよく分からなくなってしまいましたので、ご教授よろしくお願いいたします。
184
214
 
185
215
 

1

index.phpの追加。

2019/08/25 05:22

投稿

myksgo
myksgo

スコア21

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,7 @@
1
1
  ```PHP
2
2
 
3
+ //drink.php
4
+
3
5
  <?php
4
6
 
5
7
  require_once('menu.php');
@@ -48,6 +50,8 @@
48
50
 
49
51
  ```PHP
50
52
 
53
+ //food.php
54
+
51
55
  <?php
52
56
 
53
57
  require_once('menu.php');
@@ -86,6 +90,96 @@
86
90
 
87
91
  ```
88
92
 
93
+ ```php
94
+
95
+ //index.php
96
+
97
+ <?php
98
+
99
+ require_once('data.php');
100
+
101
+ require_once('menu.php');
102
+
103
+ ?>
104
+
105
+
106
+
107
+ <!DOCTYPE html>
108
+
109
+ <html>
110
+
111
+ <head>
112
+
113
+ <meta charset="utf-8">
114
+
115
+ <title>Café Progate</title>
116
+
117
+ <link rel="stylesheet" type="text/css" href="stylesheet.css">
118
+
119
+ <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'>
120
+
121
+ </head>
122
+
123
+ <body>
124
+
125
+ <div class="menu-wrapper container">
126
+
127
+ <h1 class="logo">Café Progate</h1>
128
+
129
+ <h3>メニュー<?php echo Menu::getCount() ?>品</h3>
130
+
131
+ <form method="post" action="confirm.php">
132
+
133
+ <div class="menu-items">
134
+
135
+ <?php foreach ($menus as $menu): ?>
136
+
137
+ <div class="menu-item">
138
+
139
+ <img src="<?php echo $menu->getImage() ?>" class="menu-item-image">
140
+
141
+ <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3>
142
+
143
+ <?php if ($menu instanceof Drink): ?>
144
+
145
+ <p class="menu-item-type"><?php echo $menu->getType() ?></p>
146
+
147
+ <?php else: ?>
148
+
149
+ <?php for($i=0;$i<$menu->getSpiciness();$i++):?>
150
+
151
+ <img class="icon-spiciness" src="https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/chilli.png">
152
+
153
+ <?php endfor ?>
154
+
155
+ <?php endif ?>
156
+
157
+ <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p>
158
+
159
+ <input type="text" value="0" name="<?php echo $menu->getName() ?>">
160
+
161
+ <span>個</span>
162
+
163
+ </div>
164
+
165
+ <?php endforeach ?>
166
+
167
+ </div>
168
+
169
+ <input type="submit" value="注文する">
170
+
171
+ </form>
172
+
173
+ </div>
174
+
175
+ </body>
176
+
177
+ </html>
178
+
179
+
180
+
181
+ ```
182
+
89
183
  setとgetについてよく分からなくなってしまいましたので、ご教授よろしくお願いいたします。
90
184
 
91
185
 
@@ -101,3 +195,13 @@
101
195
 
102
196
 
103
197
  どうやって説明したら良いのか分からずわかりにくい質問になり申し訳ございません。
198
+
199
+
200
+
201
+ 追記
202
+
203
+ 「ホット」や「アイス」を指定するときにget+setで定義し、「辛さ」を表すspicinessではgetのみの定義となっております。
204
+
205
+ 例えばフォームなどの入力してもらう項目に対してはsetを使い項目をsetしその項目をgetで取得しているのかなと思っておりましたが、違うようで混乱しております。
206
+
207
+ よろしくお願いいたします。