FUNKAM35 Posted October 3, 2012 Share Posted October 3, 2012 I cannot see what is wrong with this code and have spent hours trying to get it to work: if($pound==0 && $price >0 && $price <90000000){ $pound=$price*$euro; $pound=number_format($pound); $usd=$price*$usd; $usd=number_format($usd,2); $price=number_format($price); echo "<p>£ $pound approx</p><p>US $ $usd Approx </p>\n"; } The strange thing is that it works on the first item but not on subsequent items, it works every time for price and pound, it is the usd that is the problem, eg first item everything correct: € 53,000 £ 45,298 approx US $ 68,406.04 Approx second item price and pounds correct but usd crazy: € 63,000 £ 53,845 approx US $ 4,284,000.00 Approx third item price and pounds correct but usd crazy € 68,250 £ 58,332 approx US $ 273,000.00 Approx Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/269039-whats-wrong-with-this-code/ Share on other sites More sharing options...
KevinM1 Posted October 3, 2012 Share Posted October 3, 2012 Is this in a loop? If so, show the entire loop. Quote Link to comment https://forums.phpfreaks.com/topic/269039-whats-wrong-with-this-code/#findComment-1382481 Share on other sites More sharing options...
RussellReal Posted October 3, 2012 Share Posted October 3, 2012 You are compounding $usd You should if anything set $usd back to zero after the output, or it's default value, after the output! You're welcome <3 - Russell Quote Link to comment https://forums.phpfreaks.com/topic/269039-whats-wrong-with-this-code/#findComment-1382482 Share on other sites More sharing options...
FUNKAM35 Posted October 3, 2012 Author Share Posted October 3, 2012 thanks, how do i reset the usd and why does it work for punds but not usd? Quote Link to comment https://forums.phpfreaks.com/topic/269039-whats-wrong-with-this-code/#findComment-1382489 Share on other sites More sharing options...
RussellReal Posted October 3, 2012 Share Posted October 3, 2012 (edited) thanks, how do i reset the usd and why does it work for punds but not usd? Pounds doesn't use $usd in the calculation, infact it doesn't use $pound in the calculation for pounds.. I'll explain:) $pound=$price*$euro; $pound=number_format($pound); $euro never gets changed, $price changes in the increment the you want it to (from the loop) so everything is controlled properly in this $usd=$price*$usd; $usd=number_format($usd,2); $usd IS getting assigned to the value of ($price * $usd), $usd originally (before the first loop), $price is controlled it increments the way you want it to. AFTER the first loop $usd will equal something absurd and will screw up your calculations.. try this code instead: if($pound==0 && $price >0 && $price <90000000){ $pound=$price*$euro; $pound=number_format($pound); $usd2=$price*$usd; $usd2=number_format($usd2,2); $price=number_format($price); echo "<p>£ $pound approx</p><p>US $ $usd2 Approx </p>\n"; } Edited October 3, 2012 by RussellReal Quote Link to comment https://forums.phpfreaks.com/topic/269039-whats-wrong-with-this-code/#findComment-1382492 Share on other sites More sharing options...
FUNKAM35 Posted October 3, 2012 Author Share Posted October 3, 2012 RussellReal thank you so much ur a genius. please forgive my ignorance I am learning! It works fantastic, thanks again Quote Link to comment https://forums.phpfreaks.com/topic/269039-whats-wrong-with-this-code/#findComment-1382493 Share on other sites More sharing options...
RussellReal Posted October 3, 2012 Share Posted October 3, 2012 Hey, anytime, thats what we're here for! Can you please mark the topic as Solved! Thank you kindly, Russell Quote Link to comment https://forums.phpfreaks.com/topic/269039-whats-wrong-with-this-code/#findComment-1382505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.