Duke555 Posted June 8, 2009 Share Posted June 8, 2009 i have a bug in my code(below) and i cannot get rid of it. for whatever reason the $key that it is outputting is never the first key, but always the second key. i.e. i do not get to output a 'Candle Holder' even though it is the 1st key in the outer array. apart from that it seems to work fine. thank you ==================== <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) { list($key, $val) = each($product); echo "$key<br>"; while (list($key, $val) = each($name)) { echo "<li>$key => $val<br>"; } // 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> Link to comment https://forums.phpfreaks.com/topic/161376-solved-key-bug-in-php-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 8, 2009 Share Posted June 8, 2009 foreach() maintains its own array pointer and the functions each(), next(), prev() that modify the array pointer should not be used inside of a foreach() loop. Let the foreach loop advance the pointer. You should be using - foreach (array_expression as $key => $value) $key will contain the product name and $value will be an array that contains the price and shipping. Link to comment https://forums.phpfreaks.com/topic/161376-solved-key-bug-in-php-code/#findComment-851621 Share on other sites More sharing options...
Duke555 Posted June 8, 2009 Author Share Posted June 8, 2009 thank you. i just wanted to ask you if the code should look like below. p.s. you said that foreach() maintains its own pointer. meaning, is that why $key was always the next value and never the 1st one because foreach() already moved the pointer to the 2nd value even though it was the 1st loop iteration? or is that each() moved the pointer to the 2nd value? =========================== foreach($product as $name => $description) { echo "$name<br>"; while (list($key, $val) = each($name)) { echo "<li>$key => $val<br>"; } // 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>"; } Link to comment https://forums.phpfreaks.com/topic/161376-solved-key-bug-in-php-code/#findComment-851693 Share on other sites More sharing options...
Duke555 Posted June 8, 2009 Author Share Posted June 8, 2009 got it thank you below is the answer =========== foreach($product as $name => $description) { echo "$name<br>"; while (list($key, $val) = each($description)) { echo "<li>$key => $val<br>"; } // 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($description[Price], $description[shipping]); echo"</pre>"; } Link to comment https://forums.phpfreaks.com/topic/161376-solved-key-bug-in-php-code/#findComment-851734 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.