</head> <body> <p> <?php /* * Tax Calculator * Create a function that calculates the cost with tax of an item * The function must accept 2 parameters. $cost and $tax * The function must return the $meal_cost with tax * Print the results to the screen in the format: * Vince went to FL to buy some Mellow Mushroom, it costed him $106.50 */ $state_tax = array('NC' => 0.07000000000000001, 'NY' => 0.094, 'FL' => 0.065); $people = array('Eric' => array('state' => 'NC', 'meal' => 74.09999999999999, 'food' => 'Bad Daddys'), 'Vince' => array('state' => 'FL', 'meal' => 100, 'food' => 'Mellow Mushroom'), 'James' => array('state' => 'NY', 'meal' => 4500, 'food' => 'Mc Donalds')); function calculateTax($meal_cost, $tax) { $price = $meal_cost + $meal_cost * $tax; return $price; } foreach ($people as $key => $person) { $state = $person['state']; echo "{$key} went to " . $person['state'] . " to buy some " . $person['food'] . ", it cost him \$" . calculateTax($person['meal'], $state_tax[$state]) . "</br>"; } ?> </p> </body> </html>
die; } if (isset($_SESSION['errmsg'])) { echo $_SESSION['errmsg'] . "<br>"; unset($_SESSION['errmsg']); } echo "<p><h2>Thank you for shopping with us!<br>Your order ID is: " . $orderid . "</h2></p>"; showCartTable($_SESSION['cart'], TRUE); // Display read only version of shopping cart table ?> <table border="0" width="100%" > <tr><td align="left">Tax</td> <td align="right"> <?php echo number_format(calculateTax(calculateTotal($_SESSION['cart'])), 2); ?> </td></tr> <tr><td align="left">Shipping</td> <td align="right"> <?php echo number_format($_POST['shipping'], 2); ?> </td></tr> <tr><td bgcolor="black" align="left">TOTAL:</td> <td bgcolor="black" align="right"><b>$ <?php echo number_format($_POST['total'], 2); ?> </b></td> </tr> </table>
function getShoppingCart() { //Function creates the display for the cart //If the cart exists then follow on if (cartExists()) { //Check if there are any products in the cart by counting the array keys if (count($_SESSION['paypalCart']) > 0) { //The table header html $html = ' <table cellpadding="0" cellspacing="0" border="0"> <tr> <th>Product</th> <th>Price</th> <th>Qty</th> <th></th> </tr> '; $count = 1; //Loop through the items in the paypal cart foreach ($_SESSION['paypalCart'] as $product) { $html .= ' <tr> <td>' . $product['name'] . '</td> <td>€' . number_format($product['price'], 2) . '</td> <td>' . $product['qty'] . '</td> <td><a href="addToCart.php?ID=' . $product['ID'] . '">Add</a><a href="removeFromCart.php?ID=' . $product['ID'] . '">Remove</a></td> <input type="hidden" name="amount_' . $count . '" value="' . $product['price'] . '" /> <input type="hidden" name="quantity_' . $count . '" value="' . $product['qty'] . '" /> <input type="hidden" name="tax_rate_' . $count . '" value="' . TAX . '" /> <input type="hidden" name="item_name_' . $count . '" value="' . stripslashes($product['name']) . '" /> <input type="hidden" name="item_number_' . $count . '" value="' . $product['ID'] . '" /> </tr> '; $count++; } //HTML for the subrows such as the subtotal, tax and total $html .= ' <tr class="empty"> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td class="bold">Subtotal</td> <td>' . calculateSubtotal() . '</td> </tr> <tr> <td></td> <td></td> <td class="bold">TAX @ ' . TAX . '%</td> <td>' . calculateTax() . '</td> </tr> <tr> <td></td> <td></td> <td class="bold">Total</td> <td>' . calculateTotal() . '</td> </tr> </table> <input type="submit" name="submit" id="submit" value="Checkout with PayPal" /> '; echo $html; } else { //If there are no products then print out a message saying there are no products echo '<p>There are currently no products in your cart.</p>'; } } else { //If there are no products then print out a message saying there are no products echo '<p>There are currently no products in your cart.</p>'; } }
?> <table border="0" width="100%" > <tr><td align="left">Tax</td> <td align="right"> <?php echo number_format(calculateTax(calculateTotal($_SESSION['cart'])), 2); ?> </td></tr> <tr><td align="left">Shipping</td> <td align="right"> <?php echo number_format(calculateShipping($_POST['shipping']), 2); ?> </td></tr> <tr><td bgcolor="black" align="left">TOTAL:</td> <?php $total = calculateShipping($_POST['shipping']) + calculateTotal($_SESSION['cart']) + calculateTax(calculateTotal($_SESSION['cart'])); ?> <td bgcolor="black" align="right"><b>$ <?php echo number_format($total, 2); ?> </b></td> </tr> </table> <form action="receipt.php" method="post"> <!-- Messy way of keeping everything from the checkout form --> <input name="addr" type="hidden" value="<?php echo $_POST["addr"]; ?> " /> <input name="city" type="hidden" value="<?php