By using arrays like lists, we can create dynamic lists that the user can choose from. We can also count this list and know how many items we have in it.
Shirts, More shirts, and what size do you want again?
Shirts, More shirts, and what size do you want again? Part 2
Here are the sources used in this tutorial:
tut005.php
<?php //This is going to be a file about shirts $shirts = array(); $shirts[] = "XS"; $shirts[] = "S"; $shirts[] = "M"; echo "you can pick from ".count($shirts)." shirts, here are your options:"; print_r($shirts); if(isset($_REQUEST['size'])){ $size = (int)$_REQUEST['size']; if($size < 0){ $size =0; } if($size > count($shirts)-1){ $size = count($shirts)-1; } }else{ $size = 2; } if(isset($_REQUEST['quant'])){ $quant = (int)$_REQUEST['quant']; if($quant < 0){ $quant = 0; } }else{ $quant = 0; } echo "<br />"; //echo $size; echo "you ordered the shirt size:".$shirts[$size].' and you ordered '.$quant.' shirts in this size. Each shirt goes from small being 10 each to 50<br />'."\n"; $cost = ($size+1)*10*$quant; echo "you order costs $cost."; if($cost > 40){ $cost = .95*$cost; echo " You just saved 5% and now it costs $cost"; } ?>