stevenjweir Posted February 8, 2011 Share Posted February 8, 2011 Hi guys, I'm still learning PHP so bare with me please. I've written a script to change the displayed price of a product based on the currency set in a cookie. The script is: <?php if ($_COOKIE["currency"] == dollar) {echo "$";} /* if the cookie is set to Dollar, show a '$' */ else if ($_COOKIE["currency"] == euro) {echo "€";} /* or if the cookie is set to Euro, show a '€' */ else { echo "£";} /* or if the cookie isn't set to Dollar or Euro, show a '£' */ printf ('%02.2f', (($row_getdets['price']) * $row_prices['[$_COOKIE["currency"]']) ); /* Show Price multiplied by the chosen currency rate set by the cookie, set to 2 decimals. */ ?> So basically I just need it to show the currency symbol, ie: £, $ or € and then show the price. The price part works, the bit that doesn't is the if else part. Any help would be really appreciated. Link to comment https://forums.phpfreaks.com/topic/227048-help-with-if-else/ Share on other sites More sharing options...
micmania1 Posted February 8, 2011 Share Posted February 8, 2011 <?php if ($_COOKIE["currency"] == 'dollar') {echo "$";} /* if the cookie is set to Dollar, show a '$' */ else if ($_COOKIE["currency"] == 'euro') {echo "€";} /* or if the cookie is set to Euro, show a '€' */ else { echo "£";} /* or if the cookie isn't set to Dollar or Euro, show a '£' */ printf ('%02.2f', (($row_getdets['price']) * $row_prices['[$_COOKIE["currency"]']) ); /* Show Price multiplied by the chosen currency rate set by the cookie, set to 2 decimals. */ ?> You missed the quotation marks around the $_COOKIE values. ie 'dollar', 'euro'. Link to comment https://forums.phpfreaks.com/topic/227048-help-with-if-else/#findComment-1171372 Share on other sites More sharing options...
stevenjweir Posted February 8, 2011 Author Share Posted February 8, 2011 micmania1, thank you. Sorted it. Also I just noticed the price bit doesn't actually work. The line: printf ('%02.2f', (($row_getdets['price']) * $row_prices['dollar']) ); where it says dollar, I need it to grab $_COOKIE["currency"] and replace the dollar.How do I include the $_COOKIE data within that ['dollar'] box? Link to comment https://forums.phpfreaks.com/topic/227048-help-with-if-else/#findComment-1171376 Share on other sites More sharing options...
micmania1 Posted February 8, 2011 Share Posted February 8, 2011 You need to look at the problem a bit differently then. // Guessed rates $currency = array( 'gbp' => array('symbol' => '£', 'rate' => '1'), 'dollar' => array('symbol' => '$', 'rate' => '1.4'), 'euro' => array('symbol' => '€', 'rate' => '1.1') ); // Store the currency in the cookie (ie gbp, dollar etc) if (array_key_exists($_COOKIE['currency'], $currency)) { $symbol = $currency[$_COOKIE['currency']]['symbol']; $rate = $currency[$_COOKIE['currency']]['rate']; // Work out price in new currency $price = ($row_getdets['price'] * $rate); // Display price in currency echo $symbol.$rate; } else { // Cookie has invalid vlue // currency does not exist die("Invalid currency"); } Link to comment https://forums.phpfreaks.com/topic/227048-help-with-if-else/#findComment-1171383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.