<?php
        /****************
        file: meal-cost.php
        called from meal-cost.html
        as associative array
        outputs the sum of the $values and then calculates the total bill
        and the average price of the courses
        allows user to input new data
                
        **************/
        include('library2.inc');
        page_header('meal costs', 'red.css');
        $sum = 0;
        $countitem = 0;
        
        
        $entree = $_POST['entree'];
        $main = $_POST['main'];
        $dessert = $_POST['dessert'];
        if(($entree < 5) || ($entree > 80))
        {
         echo "Entree prices must be between 5 and 80";
         echo "<a href=meal-cost.html>return to form</a>";
        }
        
        else if(($main < 5) || ($main > 100))
        {
        
         echo "Mains prices must be between 5 and 100";
         echo "<a href=meal-cost.html>return to form</a>";
        }
        else if(($dessert < 5) || ($dessert > 30))
        {
        
         echo "Dessert prices must be between 5 and 30";
         echo "<a href=meal-cost.html>return to form</a>";
        }
        
        
        else
        {
        
        foreach($_POST as $course =>$cost)
        
        {
                
                $cost = number_format($cost, 2);
                echo " The $course comes to $cost<br />";
                $countitem +=1;
                $sum += $cost;
        }
        
        
        $sum = number_format($sum,2);
        echo "You pay this bill: \$$sum";
        $avg = $sum / $countitem;
        $avg = number_format($avg,2);
                
        echo "<br />The average cost of each course/choice is $". $avg. " . <br />";        
        $tip = 0.15;
        $mealcost = $sum + ($sum * $tip);
        $mealcost = number_format($mealcost, 2);
        echo "Note: US users! With a 15% tip, this is what you owe: $".$mealcost." . <br />";
        
        }
page_footer('meal-cost.html','STL');