所以我正在做一项任务,我必须订购比萨饼。我是 PHP 新手,正在努力让我的数学工作。所以这是我的 HTML
<!doctype html>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<meta http-equiv="expires" content="0" />
<html lang="en">
<head>
<title> HW05 </title>
<link rel="stylesheet" href="hw05.css">
</head>
<body>
<form method="post" action="hw05.php">
<header>
<h1 id="title">
Pizza Order
</h1>
</header>
<section id="ci">
<h2 id="ciheader">
Customer Information
</h2>
<p id="text">
Name: <input type="text" name="name" size="15" /> <br /><br />
Phone: <input type="text" name="number" size="15" /> <br /> <br />
<input type="submit" value="Order" />
</p>
</section>
<section id="size">
<h2 id="sizeheader">
Pizze Size
</h2>
<p id="radio">
<input type="radio" name="size1" value="small"> Small <br />
<input type="radio" name="size2" value="medium"> Medium <br />
<input type="radio" name="size3" value="large"> Large
</p>
</section>
<section id="top">
<h2 id="topheader">
Toppings
</h2>
<p id="checkbox">
<input type="checkbox" name="checkbox1" value="box"> Pepperoni <br />
<input type="checkbox" name="checkbox2" value="box"> Sausage <br />
<input type="checkbox" name="checkbox3" value="box"> Mushroom <br />
<input type="checkbox" name="checkbox4" value="box"> Ham <br />
<input type="checkbox" name="checkbox5" value="box"> Olives <br />
<input type="checkbox" name="checkbox6" value="box"> Onions
</p>
</section>
<section id="price">
<h2 id="priceheader">
Topping Price
</h2>
<p id="dollars">
$1.00 <br />
$1.50 <br />
$0.80 <br />
$1.25 <br />
$0.75 <br />
$0.50
</p>
</section>
</form>
</body>
</html>
因此,我需要从 HTML 创建一个 PHP 页面,该页面计算浇头的数量并将大小和浇头的总成本相加。我不知道如何让 PHP 将每个配料的成本和比萨大小的成本加在一起。我能够让 PHP 来计算浇头。任何帮助将非常感谢。
你可以这样做:
<input type="checkbox" name="topping[]" value="pepperoni"> Pepperoni <br />
<input type="checkbox" name="topping[]" value="sausage"> Sausage <br />
<input type="checkbox" name="topping[]" value="mushroom"> Mushroom <br />
<input type="checkbox" name="topping[]" value="ham"> Ham <br />
<input type="checkbox" name="topping[]" value="olives"> Olives <br />
<input type="checkbox" name="topping[]" value="onions"> Onions
在您的hw05.php
文件中:
$toppingToPriceMap = [
"pepperoni" => 1,
"sausage" => 0.8,
"mushroom" => 1.25,
"ham" => 0.75,
"olives" => 0.5,
"onions" => 0.75
];
if (isset($_POST["topping"]) {
$numberOfToppings = count($_POST["topping"]);
$cost = 0;
foreach ($_POST["topping"] as $topping) {
if (isset($toppingToPriceMap[$topping])) {
$cost += $toppingToPriceMap[$topping];
}
}
}
echo "Selected $numberOfToppings toppings at a cost of $".$cost;
注意:这只是您的代码所需的修复之一。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句