为什么这个PHP开关会起作用

时间:2013-09-12 23:20:48

标签: php html switch-statement

我正在完成这项任务,我们需要创建一个小型网络应用程序,让某人在三明治上订购他们想要的东西,然后用更简单的术语表达任何获得订单制作三明治的人。

我已经问了几个关于这个的问题,但是我遇到了一个最后的障碍,一直困难地拍摄了一个小时,并且无法弄清楚出了什么问题。如果我只是在PHP中注释掉cheesetype开关,那么一切都有效,但是一旦我重新引入它,我就会收到服务器错误。我对所有语法进行了三倍以上的检查,但是一旦切换显示出来就会变得很糟糕。我做错了什么?

HTML

<!DOCTYPE html>
<html>
<head>
<title>Sandwich Order</title>
</head>
<body>
<form name="sandwichform" method="post" action="sandwichmaker.php">
Size
<select name="size">
    <option value="small">Small</option>
    <option value="medium">Medium</option>
    <option value="large">Large</option>
</select><br/>
Meat Type
<select name="meattype">
    <option value="ham">Ham</option>
    <option value="turkey">Turkey</option>
    <option value="beef">Roast Beef</option>
    <option value="salami">Salami</option>
</select><br/>
Cheese
<select name="cheesetype">
    <option value="american">American</option>
    <option value="swiss">Swiss</option>
    <option value="jack">Monterey Jack</option>
</select><br/>
Bread Type
<select name="breadtype">
    <option value="white">White</option>
    <option value="wheat">Wheat</option>
</select><br/>
Condiments<br/>
Ketchup<input type="checkbox" name="condiments[]" value="ketchup"><br>
Mustard<input type="checkbox" name="condiments[]" value="mustard"><br>
Mayonnaise<input type="checkbox" name="condiments[]" value="mayo"><br>
BBQ Sauce<input type="checkbox" name="condiments[]" value="bbq"><br>
<b>Extras:</b><br/>
Tomato<input type="checkbox" name="extras[]" value="tomato"><br>
Lettuce<input type="checkbox" name="extras[]" value="lettuce"><br>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>

PHP

<?php

$breadtype=$_POST['breadtype'];
$size=$_POST['size'];
$meattype=$_POST['meattype'];
$cheesetype=$_POST['cheesetype'];
$condiments=$_POST['condiments'];
$extras=$_POST['extras'];

if ($breadtype == "white")
{
    $breadimage="images/whitebread.jpg";
}
elseif ($breadtype == "wheat")
{
    $breadimage="images/wheatbread.jpg";
}

$breadoutput="<img src=\"$breadimage\" />";

switch ($size) {
case "small":
    $slices=2;
    break;
case "medium":
    $slices=4;
    break;
case "large":
    $slices=6;
    break;
}

$sizeoutput=$slices ." slices of";

switch ($meattype) {
case "ham":
    $meatimage="images/ham.jpg";
    break;
case "turkey":
    $meatimage="images/turkey.jpg";
    break;
case "beef":
    $meatimage="images/turkey.jpg";
    break;
case "salami":
    $meatimage="images/salami.jpg";
    break;
}

$meatoutput="<img src=\"$meatimage\" />";

$switch ($cheesetype) {
case "american":
    $cheeseimage="images/american.jpg";
    break;
case "swiss":
    $cheeseimage="images/swiss.jpg";
    break;
case "jack":
    $cheeseimage="images/jack.jpg";
    break;
}

$cheeseoutput="<img src=\"$cheeseimage\" />";

foreach ($extras as $extra)
{
    $ei="images/" .$extra .".jpg";

    $extrasimage .= "<br/><img src=\"$ei\"/>";
}

$extrasoutput="and " .$extrasimage;

foreach ($condiments as $condiment)
{
    $ci="images/" .$condiment .".jpg";

    $condimentsimage .= "<br/><img src=\"$ci\"/>";
}

$condimentsoutput="and " .$condimentsimage;

echo $breadoutput;
echo "<br>";
echo $sizeoutput;
echo "<br>";
echo $meatoutput;
echo "<br>";
echo $cheeseoutput;
echo "<br>";
echo $extrasoutput;
echo "<br>";
echo $condimentsoutput;
?>

2 个答案:

答案 0 :(得分:3)

$switch ($cheesetype) {

应该是

switch ($cheesetype) {

删除$符号。

答案 1 :(得分:0)

应该是切换而不是$ switch