"; $gift[1] = ""; $gift[2] = ""; //Set up the array of beers, and add values $beer['Pabst']['name'] = 'Pabst'; $beer['Pabst']['price'] = 5.00; $beer['Schlitz']['name'] = 'Schlitz'; $beer['Schlitz']['price'] = 4.50; $beer['Miller']['name'] = 'Miller'; $beer['Miller']['price'] = 3; $beer['Blatz']['name'] = 'Blatz'; $beer['Blatz']['price'] = 1.5; //Define variables used later, to avoid annoying error messages. $message = ""; $total = ""; $total_order = ""; //Set up the specials, then the order form. $high = 'Pabst'; $low = 'Blatz'; $high_percentage = .6; $display_high_percent = "" . ($high_percentage*100). "%"; $low_percentage = .25; $display_low_percent = "" . ($low_percentage*100). "%"; print "

Milwaukee Beer Co.
purveyors of vice and whimsy

"; echo "
Today's special: If more than " . $display_high_percent . " of your order is for $high, get " . $display_low_percent . " off the next highest brand you order.
If the second-highest ordered beer is $low, get the discount plus a free gift!

"; print "
"; echo ""; //To adapt to changing inventory, use a foreach() loop to populate the //ordering form. foreach ($beer as $code) { echo ""; } echo ""; echo "
\$" . number_format($code['price'], 2) . "
"; //Retrieve the data from the form and put it into $user_entry $user_entry = $_GET; // Make sure the user has ordered at least one bottle of beer. $total_order = array_sum($user_entry); //If the user has ordered something, then we proceed to process the order, //Otherwise, we print a message (below). if($total_order > 0) { //Tell the customer what the order is, by amount, beer, and price. print ""; foreach ($user_entry as $Bier => $Nummer) { if ($Nummer > 0){ //To make the noun correct in number: if ($Nummer > 1) { $bottles = "bottles"; } else { $bottles = "bottle"; } //We'll have a separate line for each brand of beer ordered. $message .= ""; $total += $beer[$Bier]['price']*$Nummer; } } echo $message; //Getting the info for which beer to discount. //I'm sure there's an easier way... $sort = $user_entry; array_multisort($sort, SORT_ASC); array_pop($sort); $count = array_splice($sort, -1); foreach ($count as $key => $value) { $discount = $key; $second_highest = $value; } if (($user_entry[$high]/$total_order > $high_percentage) && ($discount == $low)) { $total = number_format($total-($user_entry[$discount])*(($beer[$discount]['price']*$low_percentage)), 2); echo ""; echo "
Your order is:
" . $Nummer . " " . $bottles . " of " . $Bier . " at $" . number_format($beer[$Bier]['price'], 2) . " each ($" . number_format($beer[$Bier]['price']*$Nummer, 2) . " total)
Your order qualifies for a discount!
" . $discount . " is now $" . round(($beer[$discount]['price']*(1-$low_percentage)), 2) . " a bottle, for a total of $" . round($user_entry[$discount]*(($beer[$discount]['price']*(1-$low_percentage))), 2) . " this order!
Your total amount owed (with discount): $" . number_format($total, 2) . "
"; $g = count($gift); $n = rand(0, $g-1); echo "
Don't forget your gift:
"; echo $gift[$n]; echo "
"; } elseif (($user_entry[$high]/$total_order > $high_percentage) && ($second_highest > 0)) { $total = round($total-($user_entry[$discount])*(($beer[$discount]['price']*$low_percentage)), 2); echo "Your order qualifies for a discount!
" . $discount . " is now $" . round(($beer[$discount]['price']*(1-$low_percentage)), 2) . " a bottle, for a total of $" . number_format($user_entry[$discount]*(($beer[$discount]['price']*(1-$low_percentage))), 2) . " this order!"; echo "Your total amount owed (with discount): $" . number_format($total, 2) . ""; }else { // In case no discount applies. echo "Your total amount owed: $" . number_format($total, 2) . ""; }} else { //If the user hasn't submitted anything, instructions are printed. echo "
Please order some fine Milwaukee beer!
"; } ?>

Valid XHTML 1.0!