Error when iterating through array
Hey guys im trying to run through an array of checkboxes to add up a price and keep getting an unexpected } error and i cant figure out why:
$price;
foreach($_POST['extras'] as $extra)
开发者_开发问答 {
if($extra == "Deodoriser"){
$price = $price + 10;
} elseif($extra == "Carpet Protector (5 litre)"){
$price = $price + 20
} elseif($extra == "Carpet Repair Tools"){
$price = $price + 30
} elseif($extra == "Furniture Moving Equipment"){
$price = $price + 40
} elseif($extra == "Furniture Tabs"){
$price = $price + 50
} elseif($extra == "Urine Decontamination Treatment"){
$price = $price + 60 }
}
I now get this error Parse error: syntax error, unexpected T_FOREACH in /home/fevsdiet/public_html/diycarpetcare.co.uk/booking/booking.event.processing.php on line 45
if($extra == "Deodoriser"){
$price = $price + 10;
} elseif($extra == "Carpet Protector (5 litre)") {
$price = $price + 20;
} elseif($extra == "Carpet Repair Tools") {
$price = $price + 30;
} elseif($extra == "Furniture Moving Equipment") {
$price = $price + 40;
} elseif($extra == "Furniture Tabs") {
$price = $price + 50;
} elseif($extra == "Urine Decontamination Treatment") {
$price = $price + 60;
} else {
$price = 0;
}
Alternative:
foreach($_POST['extras'] as $extra) {
switch($extra) {
case 'Deodoriser':
$price = $price + 10;
break;
case 'Carpet Protector (5 litre)':
$price = $price + 20;
break;
case 'Carpet Repair Tools':
$price = $price + 30;
break;
case 'Furniture Moving Equipment':
$price = $price + 40;
break;
case 'Furniture Tabs':
$price = $price + 50;
break;
case 'Urine Decontamination Treatment':
$price = $price + 60;
break;
// Could add a default as well
default:
$price = 0;
}
}
精彩评论