sunnyboy1984
Mar 1st 2007, 4:53 am
Can anyone fix this code by creating as many functions as needed to eliminate repetition and utilize code reusability? Please comment where neccessary. Thank you.
<B>Checkout</B><br>
Below is a summary of the products you wish to purchase, along with totals:
<?php
#tax rate is constant
$tax = 0.08;
$total_price = 0;
$total_tax = 0;
$total_shipping = 0;
$grand_total = 0;
?><ul><?
$product = "Candle Holder";
$price = 12.95;
$shipping = 0; //free shipping
echo "<li>".$product.": $".$price;
//tally totals
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total += ($total_price + $total_tax + $total_shipping);
$product = "Coffee Table";
$price = 99.50;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;
//tally totals
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total += ($total_price + $total_tax + $total_shipping);
$product = "Lamp";
$price = 42.99;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;
//tally totals
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total += ($total_price + $total_tax + $total_shipping);
?>
</ul>
<hr>
Subtotal: $<? echo number_format($total_price, 2); ?><br>
Tax: $<? echo number_format($total_tax, 2); ?><br>
Shipping: $<? echo number_format($total_shipping, 2); ?><br>
<B>Total: $<? echo number_format($grand_total, 2); ?></B>
<B>Checkout</B><br>
Below is a summary of the products you wish to purchase, along with totals:
<?php
#tax rate is constant
$tax = 0.08;
$total_price = 0;
$total_tax = 0;
$total_shipping = 0;
$grand_total = 0;
?><ul><?
$product = "Candle Holder";
$price = 12.95;
$shipping = 0; //free shipping
echo "<li>".$product.": $".$price;
//tally totals
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total += ($total_price + $total_tax + $total_shipping);
$product = "Coffee Table";
$price = 99.50;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;
//tally totals
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total += ($total_price + $total_tax + $total_shipping);
$product = "Lamp";
$price = 42.99;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;
//tally totals
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total += ($total_price + $total_tax + $total_shipping);
?>
</ul>
<hr>
Subtotal: $<? echo number_format($total_price, 2); ?><br>
Tax: $<? echo number_format($total_tax, 2); ?><br>
Shipping: $<? echo number_format($total_shipping, 2); ?><br>
<B>Total: $<? echo number_format($grand_total, 2); ?></B>