Henry Edward Quinn IV
Henry Edward Quinn IV

Reputation: 633

Why is this PHP switch acting up

I'm working on this assignment where we need to create a little web app to have someone order what they want on a sandwich, and then put it in simpler terms for whoever gets the order to make the sandwich.

I've asked a couple questions about this already, but I hit one final roadblock, have been trouble shooting it for an hour, and can't figure out what's wrong. Everything works if I just comment out the cheesetype switch in the PHP, but as soon as I reintroduce it, I get a server error. I more than triple checked all the syntax, but as soon as that switch shows up things get wonky. What did I do wrong?

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;
?>

Upvotes: 1

Views: 114

Answers (2)

seven
seven

Reputation: 115

it should be switch not $switch

Upvotes: 0

Lock
Lock

Reputation: 5522

$switch ($cheesetype) {

should be

switch ($cheesetype) {

Drop the $ sign.

Upvotes: 3

Related Questions