前提・実現したいこと
ProgateのPHP、オブジェクト指向のあたりをやっていますが、
一点わからないところがあったので質問させてください。
発生している問題・エラーメッセージ
$orderCount = $_POST[$menu->getName()]; これは文字列が入っていませんか? この後$orderCountを引数にして、MenuクラスのプロパティにorderCountがsetして、 計算されるわけですが、この$orderCountがなぜ数値になるのか教えていただきたいです。
該当のソースコード
PHP
1<?php 2class Menu { 3 private $name; 4 private $price; 5 private $image; 6 private $orderCount = 0; 7 8 public function __construct($name, $price, $image) { 9 $this->name = $name; 10 $this->price = $price; 11 $this->image = $image; 12 } 13 14 public function hello() { 15 echo '私は'.$this->name.'です'; 16 } 17 18 public function getName() { 19 return $this->name; 20 } 21 22 public function getImage() { 23 return $this->image; 24 } 25 26 public function getOrderCount() { 27 return $this->orderCount; 28 } 29 30 public function setOrderCount($orderCount) { 31 $this->orderCount = $orderCount; 32 } 33 34 public function getTaxIncludedPrice() { 35 return floor($this->price * 1.08); 36 } 37 38 public function getTotalPrice() { 39 return $this->orderCount * $this->getTaxIncludedPrice(); 40 } 41 42} 43 44$juice = new Menu('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png'); 45$coffee = new Menu('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png'); 46$curry = new Menu('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png'); 47$pasta = new Menu('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png'); 48 49$menus = array($juice, $coffee, $curry, $pasta); 50 51?> 52<!DOCTYPE html> 53<html> 54<head> 55 <meta charset="utf-8"> 56 <title>Progate</title> 57 <link rel="stylesheet" type="text/css" href="stylesheet.css"> 58 <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'> 59</head> 60<body> 61 <div class="order-wrapper"> 62 <h2>注文内容確認</h2> 63 <?php foreach ($menus as $menu): ?> 64 <?php 65 $orderCount = $_POST[$menu->getName()]; 66 $menu->setOrderCount($orderCount); 67 68 ?> 69 <p class="order-amount"> 70 <?php echo $menu->getName() ?> 71 x 72 <?php echo $orderCount ?> 73 個 74 </p> 75 <p class="order-price"><?php echo $menu->getTotalPrice(); ?>円</p> 76 <?php endforeach ?> 77 </div> 78</body> 79</html>
HTML
1<?php require_once('data.php') ?> 2 3<!DOCTYPE html> 4<html> 5<head> 6 <meta charset="utf-8"> 7 <title>Café Progate</title> 8 <link rel="stylesheet" type="text/css" href="stylesheet.css"> 9 <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'> 10</head> 11<body> 12 <div class="menu-wrapper container"> 13 <h1 class="logo">Café Progate</h1> 14 <form method="post" action="confirm.php"> 15 <div class="menu-items"> 16 <?php foreach ($menus as $menu): ?> 17 <div class="menu-item"> 18 <img src="<?php echo $menu->getImage() ?>" class="menu-item-image"> 19 <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3> 20 <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p> 21 <input type="text" value="0" name="<?php echo $menu->getName() ?>"> 22 <span>個</span> 23 </div> 24 <?php endforeach ?> 25 </div> 26 <input type="submit" value="注文する"> 27 </form> 28 </div> 29</body> 30</html>
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー