Chapter 2 - Pursue Pizza Order Form

Objective: Create a new array and then display its elements. Sort the array in different ways and then display the array’s contents again. Create a form that contains a select menu or series of check boxes that allow for multiple sections. Then, in the handling PHP script, display the selected items along with a count of how many the user selected. For added complexity, take the suggested PHP script you just created (that handles multiple selections), and have it display the selections in alphabetical order.

Output
Build your pizza!


Small $10.50
Medium $12.50
Large $14.50


Pepperoni +$1.50
Extra cheese +$1.25
Broccoli +$.75
Pineapple +$1.50
Garlic +$.50
Mushrooms +$.75

Source
<?php
$sizes = array(
	'small'=>'10.50',
	'medium'=>'12.50',
	'large'=>'14.50'
);
$addons = array(
	'pepperoni'=>'1.50',
	'extra cheese'=>'1.25',
	'broccoli'=>'.75',
	'pineapple'=>'1.50',
	'garlic'=>'.50',
	'mushrooms'=>'.75'
);
$order_submit = (isset($_POST['submit']))?$_POST['submit']:null;
$sort_submit = (isset($_POST['sort']))?$_POST['sort']:null;
$size = (isset($_POST['size']))?$_POST['size']:null;
$toppings=(isset($_POST['toppings']))?$_POST['toppings']:null;
$sort_order = (isset($_POST['sort-order']))?$_POST['sort-order']:null;
$error = '';
$error_sort = '';

if($_SERVER['REQUEST_METHOD']=='POST' && $order_submit){
	if($size) {
		$message = '';
		$cost = $sizes[$_POST['size']];
		$message .= "Thanks for your order!<br />";
		$message .= 'You selected a '.((!$toppings)?'plain ':'').$_POST['size'].' pizza';
		if($toppings) {
			$message .= ' with the following '.((count($toppings)>1)?count($toppings):'').' topping'.((count($toppings)>1)?'s':'').':';
			$message .= '<ol>';
			asort($toppings);
			foreach($toppings as $topping){
				$message .= '<li>'.ucfirst($topping).'</li>';
				$cost = $cost + $addons[$topping]; // using $cost.= yields string concatenation not numerical addition!
			}
			$message .= '</ol>';
		} else {
			$message .= '.<br/>';
		}
		echo '<div class="form-results">'.$message.'Your order total is <b>$'.number_format($cost,2).'</b> .</div>';
	} else {
		$error .= '<span class="error">Your order cannot be completed without selecting a size! Please resubmit your order.</span>'; 
	}
} ?>
<?php
if($_SERVER['REQUEST_METHOD']=='POST' && $sort_submit){
	if($sort_order) { //sort toppings array according to user select
		switch ($sort_order) {
			case 'alphabetical':
				ksort($addons);
			break;
			case 'priceAsc':
				asort($addons);
			break;
			case 'priceDsc':
				arsort($addons);
			break;
			default:
			break;
		}
	} else { //user did not select anything
		$error_sort = '<span class="error">Please select a sort order!</span>';
	}
} ?>
<form  name="order-form" method="post" action="#">
	<fieldset><legend>Build your pizza!</legend>
		<p>
			<label>Size</label><br />
			<?php foreach($sizes as $key=>$value) {
				echo '<input name="size" value="'.$key.'" type="radio"/>'.ucfirst($key).' $'.$value.'<br />';
			}
			if(!empty($error)) echo $error;
			?>
		</p>
		<p>
			<label>Toppings<?php echo ($sort_order)?' ('.$sort_order.')':'';?></label><br />
			<?php foreach($addons as $key=>$value) {
					echo '<input name="toppings[]" value="'.$key.'" type="checkbox" />'.ucfirst($key).' +$'.$value.'<br />';
			} ?>
			<select name="sort-order"> 
		      <option value="0">Select Sort Order</option>
			  <option value="alphabetical" >Alphabetical</option>
			  <option value="priceAsc" >Price (low->high)</option>
			  <option value="priceDsc" >Price (high->low)</option>
			</select>
			<input type="submit" name="sort" value="Sort Toppings" />
			<?php echo ($error_sort)?$error_sort:'';?>
		</p>
	</fieldset>
	<p>
		<input type="submit" name="submit" value="Place my order" />
	</p>
</form>