i have the following bug in my code (see below).
what i am trying to print values of the array it starts with a second value
i.e. 'coffee table' as opposed to a 'Candle Holder'. when i was using list($key, $val) = each($product); and then echo $key; it was exactly the same thing.
thank you 🙂
here is the output:
Checkout
Below is a summary of the products you wish to purchase, along with totals:
Coffee Table
* Price => 12.95
* Shipping => 0
Lamp
* Price => 99.5
* Shipping => 0.1
* Price => 42.99
* Shipping => 0.1
Total (including tax and shipping): $182.12
Code:
===================================
<B>Checkout</B><br>
Below is a summary of the products you wish to purchase, along with totals:
<?php
$total_price = 0;
$total_tax = 0;
$total_shipping = 0;
$grand_total = 0;
?><ul><?
//2 dimensional array that acts as a DB and stores Name, Price and Shipping (as a % of price) for every given item,
$product = array('Candle Holder'=> array(
Price => 12.95,
Shipping => 0.0 //free shipping
),
'Coffee Table'=> array(
Price => 99.50,
Shipping => 0.10 //shipping as a % of price
),
'Lamp'=> array(
Price => 42.99,
Shipping => 0.10 //shipping as a % of price
));
reset($product);
foreach($product as $name)
{
echo"<pre>";
//list($key, $val) = each($product);
//echo "$key<br>";
echo key($product)."<br>";
next($product);
while (list($key, $val) = each($name))
{
echo "<li>$key => $val";
}
// accessing Price and Shipping for each inner array and sending these values to a calc_total
// function which is going to make a total calculation
$grand_total += calc_total($name[Price], $name[Shipping]);
echo"</pre>";
}
/
Purpose: function CALCulates a total (incl. tax) price payable by the consumer a given item
INPUT: price and shiping for each item
OUTPUT: total price which includes tax
/
function calc_total($price, $shipping)
{
#tax rate is constant
$tax = 0.08;
$total_price += $price;
$total_tax += $tax $price;
$total_shipping += $shipping $price;
$grand_total = ($total_price + $total_tax + $total_shipping);
return $grand_total;
}
?>
</ul>
<hr>
<br>
<B>Total (including tax and shipping): $<? echo number_format($grand_total, 2); ?></B>