I'm trying to convert this into 2d array using while, list, each...
This is what I got so far but I got stock? Any ideas?
Code:
<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><?
//A function that does the calculations
function calculation($price, $shipping, $tax) {
$total_price += $price;
$total_tax += $tax * $price;
$total_shipping += $shipping * $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
return $grand_total;
}
//Items
$product = "Candle Holder";
$price = 11.95;
$shipping = 0; //free shipping
echo "<li>".$product.": $".$price;
$grand_total += calculation($price, $shipping, $tax);
$product = "Coffee Table";
$price = 99.50;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;
$grand_total += calculation($price, $shipping, $tax);
$product = "Floor Lamp";
$price = 44.99;
$shipping = 0.10; //shipping as a percentage of price
echo "<li>".$product.": $".$price;
$grand_total += calculation($price, $shipping, $tax);
?>
<hr>
<br>
<B>Total (including tax and shipping): $<? echo number_format($grand_total, 2); ?></B>
Code:
$products = array( array( product => 'Candle Holder',
price => 11.95,
shipping => 0
),
array( product => 'Coffee Table',
price => 99.50,
shipping => 0.10
),
array( product => 'Floor Lamp',
price => 44.99,
shipping => 0.10
)
);
for ( $row = 0; $row < 3; $row++ )
{
while (list($key, $value ) = each($products[$row]))
{
echo " | $value";
}
echo ' <br />';
}
Comment