Progateという学習サイトで、レストランの注文システムをPHPで実装しています。
注文ページ(index.php)にて
php
1<input type="text" value="0" name="<?php echo $menu->getImage() ?>">
として、入力内容確認ページ(confirm.php)にて
php
1<?php $orderCount=$_POST[$menu->getImage()] ?>
とすると、
Notice: Undefined index: https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png in /home/progate/confirm.php on line 18
**JUICE x
Fatal error: Uncaught Error: Function name must be a string in /home/progate/confirm.php:24 Stack trace: #0 {main} thrown in /home/progate/confirm.php on line 24**
というエラーを吐きました。
一方、"getImage"を"getName"に変え、(index.php)を
php
1<input type="text" value="0" name="<?php echo $menu->getName() ?>">
とし、(confirm.php)を
php
1<?php $orderCount=$_POST[$menu->getImage()] ?>
とすると、上手く入力値が(confirm.php)に渡されていました。
入力値を渡す際、inputのname属性と"$POST_[]"の中身を同じ値にしなければならないのは分かっています。
ですが、$menu->getName()「nameプロパティ」では出来て、$menu->getImage()「imageプロパティ」では出来ない理由が分かりません。
以下、全コードを貼っておきます。
(index.php)
php
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->getImage() ?>"> /*←当該コード部分*/ 22 <span>個</span> 23 </div> 24 <?php endforeach ?> 25 </div> 26 <input type="submit" value="注文する"> 27 </form> 28 </div> 29</body> 30</html>
(confirm.php)
php
1<?php require_once('data.php') 2?> 3 4<!DOCTYPE html> 5<html> 6<head> 7 <meta charset="utf-8"> 8 <title>Progate</title> 9 <link rel="stylesheet" type="text/css" href="stylesheet.css"> 10 <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'> 11</head> 12<body> 13 <div class="order-wrapper"> 14 <h2>注文内容確認</h2> 15 <?php foreach ($menus as $menu): ?> 16 <?php $orderCount=$_POST[$menu->getImage()] ?> /*←当該コード部分*/ 17 <p class="order-amount"> 18 <?php echo $menu->getName()?> 19 x 20 <?php echo $orderCount()?> 21 個 22 </p> 23 <?php endforeach ?> 24 </div> 25</body> 26</html>
無関係だとは思いますが、(menu.php)
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} 39?>
(data.php)
php
1<?php 2require_once('menu.php'); 3 4$juice = new Menu('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png'); 5$coffee = new Menu('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png'); 6$curry = new Menu('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png'); 7$pasta = new Menu('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png'); 8 9$menus = array($juice, $coffee, $curry, $pasta); 10 11?>
css
1/* 共通のCSS */ 2* { 3 box-sizing: border-box; 4} 5 6html, body, 7ul, ol, li, 8h1, h2, h3, h4, h5, h6, p, 9form, input, div { 10 margin: 0; 11 padding: 0; 12} 13 14body { 15 font-family: Lato, 'Hiragino Kaku Gothic Pro', sans-serif; 16 font-weight: 400; 17 -webkit-font-smoothing: antialiased; 18 font-size: 14px; 19 letter-spacing: 0.05em; 20 color: #89949e; 21} 22 23h2, h3, h4, h5, h6 { 24 font-weight: 400; 25} 26 27a { 28 text-decoration: none; 29 color: #4ac0b9; 30} 31 32a:hover { 33 text-decoration: underline; 34} 35 36a:visited { 37 color: #4ac0b9; 38} 39 40img { 41 width: 100%; 42} 43 44.container { 45 width: 1170px; 46 padding-right: 15px; 47 padding-left: 15px; 48 margin-right: auto; 49 margin-left: auto; 50} 51 52/* メニュー一覧 */ 53.logo { 54 font-family: Pacifico, sans-serif; 55 font-size: 40px; 56 color: #4ac0b9; 57 display: inline-block; 58 padding: 20px 0; 59 margin: 80px 0 30px; 60 border-top: 5px solid #42c7c1; 61 border-bottom: 5px solid #42c7c1; 62} 63 64.menu-wrapper { 65 margin-bottom: 60px; 66 text-align: center; 67} 68 69.menu-item-name { 70 line-height: 1.7; 71 font-size: 25px; 72 margin-top: 15px; 73 color: #4ac0b9; 74} 75 76.price { 77 margin: 15px 0; 78 font-size: 18px; 79 color: #322f33; 80} 81 82.menu-items { 83 margin-bottom: 60px; 84} 85 86.menu-item { 87 display: inline-block; 88 width: 40%; 89 padding: 20px; 90 margin-top: 40px; 91} 92 93.menu-item-image { 94 border-radius: 5px; 95} 96 97.menu-item span { 98 font-size: 16px; 99} 100 101input { 102 margin-left: 20px; 103 margin-right: 10px; 104 padding: 5px; 105 text-align: center; 106 width: 60px; 107 font-size: 14px; 108 margin-top: 10px; 109} 110 111input[type="submit"] { 112 display: inline-block; 113 width: 160px; 114 height: 48px; 115 line-height: 48px; 116 padding: 0 20px; 117 border-radius: 30px; 118 border-style: none; 119 color: white; 120 background-color: #42c7c1; 121 font-size: 15px; 122 letter-spacing: 0.1em; 123} 124 125/* 注文確認 */ 126.order-wrapper { 127 text-align: center; 128 background-color: #f8f8f8; 129 line-height: 2; 130 letter-spacing: 0.1em; 131 width: 500px; 132 margin: 60px auto; 133 padding: 20px 0px 30px; 134 border-radius: 5px; 135 box-shadow: 0px 1px 4px #ccd7d4; 136} 137 138.order-wrapper h2 { 139 font-size: 32px; 140 margin: 30px 0; 141 color: #322f33; 142} 143 144.order-wrapper p { 145 display: inline-block; 146 font-size: 22px; 147} 148 149.order-wrapper h3 { 150 border-top: 1px solid #dae1e7; 151 margin-top: 30px; 152 padding-top: 30px; 153 font-size: 28px; 154 color: #42c7c1; 155} 156 157.order-wrapper h4 { 158 margin-top: 40px; 159 font-size: 25px; 160 color: #322f33; 161} 162 163.order-amount { 164 width: 40%; 165 text-align: left; 166} 167 168.order-price { 169 width: 35%; 170 text-align: right; 171}
よろしくお願いいたします。
回答4件
あなたの回答
tips
プレビュー