jonnystudent Posted March 10, 2012 Share Posted March 10, 2012 Hello All, I'm Jonnystudent and I'm a newbie to these forums. I've been developing a site but I've got to a stage in where I want to update the prices on a site through a javascript based drop down list. I've looked through the Search and have not found anything which I feel will help me. I've seen a few examples which I think I could implement but these methods would don't exactly fit into what I've done so far. The drop down list will feature five currencies. On selection of a currency, I want the page to reload with that particular currency and then remember the currency which has been selected. So it will kind of feature a cookie. I've tried to imitate the currency selection similar to my-wardrobe.com Infact, my feature will be almost identical. My only stumbling block is the PHP. Can anyone help me with this implementation with any examples at all. I've gotten as far as calling the currencies through json. <script type="text/javascript">// <![CDATA[ $(document).ready(function(){ fx.base = "GBP"; fx.settings = { from : "GBP" }; var amount = $Price; $.getJSON( 'http://openexchangerates.org/latest.json', function(data) { // Check money.js has finished loading: if ( typeof fx !== "undefined" && fx.rates ) { fx.rates = data.rates; fx.base = data.base; } else { // If not, apply to fxSetup global: var fxSetup = { rates : data.rates, base : data.base } } // now that we have exchange rates, add a few to our page var USD = fx.convert(amount, {to: "USD"}); //13.22784197768393 var EUR = fx.convert(amount, {to: "EUR"}); //8.567532636985659 var JPY = fx.convert(amount, {to: "JPY"}); //1028.1670562349989 // we can now use the accounting.js library to format the numbers properly USD = accounting.formatMoney(USD, "$ ", 2, ",", "."); GBP = accounting.formatMoney(GBP, "£ ", 2, ",", "."); JPY = accounting.formatMoney(JPY, "¥ ", 2, ",", "."); } ); }); // ]]></script> Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/ Share on other sites More sharing options...
trq Posted March 10, 2012 Share Posted March 10, 2012 Where exactly are you stuck? You haven't posted any PHP code. Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1325808 Share on other sites More sharing options...
jonnystudent Posted March 10, 2012 Author Share Posted March 10, 2012 Hi Thorpe, thanks for the reply. I'm a PHP newbie. I'm struggling to find the exact code to start this process on a PHP level to tell you the truth. Which is why I'm here. Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1325836 Share on other sites More sharing options...
dragon_sa Posted March 10, 2012 Share Posted March 10, 2012 Hi Johnny You need to know something about php and java script, PHP is executed on the Server - meaning it runs all its scripts then generates the HTML JavaScript is executed on the client side - meaning with the HTML code It is not possible to execute a php script after a java script, what you need to do is reload the page using javascript and not php. You can process any variables with php if you wish on reload, but again this must be done before any javascript is executed again. It would then be possible to set your cookies with php if thats what you want to do. For more javascript help take thsi question to the javascript section of this site or try at dynamic drive forums http://www.phpfreaks.com/forums/index.php?board=6.0 http://www.dynamicdrive.com/forums/forumdisplay.php?f=5 Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1325849 Share on other sites More sharing options...
jonnystudent Posted March 10, 2012 Author Share Posted March 10, 2012 Hi Dragon, thanks for the reply! I'm wondering whether im approaching this in the right way. Say I wanted to reload the page through PHP and set the cookies! As I've mentioned, the my-wardrobe.com site is exactly what I want! If you Firebug it, you can see it goes to the server side on selecting a currency! I'm just wondering how this is done! It also looks like from the string it is enabling the cookie for that particular currency! Cheers, Jonah Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1325852 Share on other sites More sharing options...
dragon_sa Posted March 10, 2012 Share Posted March 10, 2012 These are links to php pages <div id="currency_changer" style="display: none;"> <span>Currency:</span> <a href="http://www.my-wardrobe.com/core/call_backs/set_user_currency.php?c=GBP&rdt=aHR0cDovL3d3dy5teS13YXJkcm9iZS5jb20v"> £ GBP</a> <a href="http://www.my-wardrobe.com/core/call_backs/set_user_currency.php?c=USD&rdt=aHR0cDovL3d3dy5teS13YXJkcm9iZS5jb20v"> $ USD</a> <a href="http://www.my-wardrobe.com/core/call_backs/set_user_currency.php?c=EURO&rdt=aHR0cDovL3d3dy5teS13YXJkcm9iZS5jb20v"> € EURO</a> <a class="hi" href="http://www.my-wardrobe.com/core/call_backs/set_user_currency.php?c=AUD&rdt=aHR0cDovL3d3dy5teS13YXJkcm9iZS5jb20v"> $ AUD</a> <a href="http://www.my-wardrobe.com/core/call_backs/set_user_currency.php?c=NOK&rdt=aHR0cDovL3d3dy5teS13YXJkcm9iZS5jb20v"> you can see here it is passing 2 $_GET variables to a page called set_user_currency.php, they are c being the select country and rdt which is some cookie or session code. They dont have much to do with the script you posted but they do set the variable on the set_user_currency.php page of $_GET['c'] and $_GET['rdt'] to the respective values in the links, this can then be interpreted by php and then included in any generated javascript on the page by referencing those to variables, or using them to set a cookie or a session. eg <?php $country = $_GET['c']; setcookie("userCountry", $country); ?> Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1325858 Share on other sites More sharing options...
jonnystudent Posted March 11, 2012 Author Share Posted March 11, 2012 Aaah ok, I'm kind of understanding you. So to start off the script, I would have to get the rates I presume... <?php function currency($from_Currency,$to_Currency,$amount) { $amount = urlencode($amount); $from_Currency = urlencode($from_Currency); $to_Currency = urlencode($to_Currency); $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency"; $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $rawdata = curl_exec($ch); curl_close($ch); $data = explode('"', $rawdata); $data = explode(' ', $data['3']); $var = $data['0']; return round($var,3); } ?> Then I'd have to set the countries and add the base rate I suppose <?php $country = $_GET['cu']; setcookie("userCountry", $country); $base = $GBP ([ $currency = $cu = $GBP ['GBP']; $cu = $USD ['USD']; $cu = $EURO ['EURO']; $cu = $JPY ['JPY']; $cu = $AUD ['AUD']; $cu = $CHF ['CHF']; //Then I'd have set the cookies against the currencies.. if (!isset($_COOKIE['rdt'])) { $decryptedID = base64_decode($_COOKIE['rdt']); $id_currency = explode("nm2c0c4y3dn3727553", $decryptedID); ????????????????? ?> My brain has just dribbled out of my nose at this stage.. I'm finding it hard how to link the currencies to the cookies and for them to reloaded to a different page....Someone please helllllllllllllllllp Cheers, Jonah Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326089 Share on other sites More sharing options...
cpd Posted March 11, 2012 Share Posted March 11, 2012 Hi Johnny You need to know something about php and java script, PHP is executed on the Server - meaning it runs all its scripts then generates the HTML JavaScript is executed on the client side - meaning with the HTML code It is not possible to execute a php script after a java script, what you need to do is reload the page using javascript and not php. You can process any variables with php if you wish on reload, but again this must be done before any javascript is executed again. It would then be possible to set your cookies with php if thats what you want to do. For more javascript help take thsi question to the javascript section of this site or try at dynamic drive forums http://www.phpfreaks.com/forums/index.php?board=6.0 http://www.dynamicdrive.com/forums/forumdisplay.php?f=5 Read up on AJAX (Asynchronous JavaScript and XML). You can execute PHP pages using HTTP requests in JavaScript which is essentially what AJAX is. Additionally, I would stay away from cookies as recent legislation states you must have permission from the client to store cookies on their device. Granted the majority of people probably won't understand or even care about cookies but the point still stands. @Jonny - Until you have an understanding of PHP and JavaScript I wouldn't attempt AJAX. It's probably a little too complex; but if you like a serious challenge then why not =P. Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326098 Share on other sites More sharing options...
jonnystudent Posted March 11, 2012 Author Share Posted March 11, 2012 Hi CPD, thanks for pointing me in the right direction. Having looked at the my-wardrobe site and the URL's. I'm still confused to know how on changing the currency. 1) The URL at the top stays exactly the same. 2) I'm unsure of how to output the correct currency in PHP. 3) I'm unsure on how to link this to cookies. (Even more unsure through your revelation) How else could I do this. Can someone please help me in how to do this please. I've got here so far. <?php $r = new HttpRequest('http://example.com/currency_setter.php', HttpRequest::METH_POST); $r->setOptions(array('cookies' => array('lang' => 'de'))); I'm completely friggin lost. Please help. Jonah Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326178 Share on other sites More sharing options...
cpd Posted March 11, 2012 Share Posted March 11, 2012 Like I said Jonny you don't really want to be getting into AJAX unless you understand both JavaScript and PHP, or something similar. Do you want to dynamically change the currencies without the page refreshing? It would be ideal if you had the page refresh as it means you can use PHP more easily. Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326181 Share on other sites More sharing options...
jonnystudent Posted March 11, 2012 Author Share Posted March 11, 2012 Hi CPD, thanks for reply. I don't mind the page refresh, so I'd be happy without the AJAX. Cheers, Jonah Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326183 Share on other sites More sharing options...
jonnystudent Posted March 12, 2012 Author Share Posted March 12, 2012 Hi CPD, thanks for pointing me in the right direction. Having looked at the my-wardrobe site and the URL's. I'm still confused to know how on changing the currency. 1) The URL at the top stays exactly the same. 2) I'm unsure of how to output the correct currency in PHP. 3) I'm unsure on how to link this to cookies. (Even more unsure through your revelation) How else could I do this. Can someone please help me in how to do this please. I've got here so far. <?php $r = new HttpRequest('http://example.com/currency_setter.php', HttpRequest::METH_POST); $r->setOptions(array('cookies' => array('lang' => 'de'))); I'm completely friggin lost. Please help. Jonah Ok, I can see the answer to question 1 below. <?php function getUrlWithout($getNames){ $url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; $questionMarkExp = explode("?", $url); $urlArray = explode("&", $questionMarkExp[1]); $retUrl=$questionMarkExp[0]; $retGet=""; $found=array(); foreach($getNames as $id => $name){ foreach ($urlArray as $key=>$value){ if(isset($_GET[$name]) && $value==$name."=".$_GET[$name]) unset($urlArray[$key]); } } $urlArray = array_values($urlArray); foreach ($urlArray as $key => $value){ if($key<sizeof($urlArray) && $retGet!=="") $retGet.="&"; $retGet.=$value; } return $retUrl."?".$retGet; } ?> Can anyone help me with the other questions. Would be very grateful. Cheers, Jonah Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326352 Share on other sites More sharing options...
jonnystudent Posted March 12, 2012 Author Share Posted March 12, 2012 Ok, I think I'm getting somewhere...Is this code below correct? <?php session_start(); if(isset($_GET['c'])) { $currency = $_GET['c']; $allowable_currencies = currency_array( $currency_array [GBP] = 1.00; $currency_array [uSD] = 1.56362 ; $currency_array [EURO]= 1.18894; $currency_array [JPY] = 128.651; $currency_array [AUD] = 1.48711; $currency_array [CHF] = 1.43373; ); if(in_array($currency, $allowable_currencies)) { $_SESSION['rdt'] = $currency; } } if(!isset($_SESSION['rdt'])) { $_SESSION['rdt'] = 'GBP'; } ?> Any help would be greatly appreciated. Cheers, Jonah Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326600 Share on other sites More sharing options...
jonnystudent Posted March 13, 2012 Author Share Posted March 13, 2012 I've altered my code somewhat as I realised that my previous code was not an array. So my new code is.. if(isset($_GET['c'])) { $currency = $_GET['c']; $currency_array = array( $currency_array [GBP] = 1.00; $currency_array [uSD] = 1.56362 ; $currency_array [EURO]= 1.18894; $currency_array [JPY] = 128.651; $currency_array [AUD] = 1.48711; $currency_array [CHF] = 1.43373; ); if(array($currency, $currency_array)) { $_SESSION['rdt'] = $currency; } } if(!isset($_SESSION['rdt'])) { $_SESSION['rdt'] = 'GBP'; } ?> <span>Currency:</span><a href="http://x/Currency_script.php?c=GBP" class="hi"> £ GBP</a><a href="http://x/Currency_script.php?c=USD"> $ USD</a><a href="http://x/Currency_script.php?c=EURO"> € EURO</a><a href="http://x/Currency_script.php?c=JPY"> ¥ JPY</a><a href="http://x/Currency_script.php?c=AUD"> $ AUD</a><a href="http://x/Currency_script.php?c=CHF"> <span class="currency_sign">Fr</span> CHF</a> Can anyone tell me if this would reload the page with new prices and store it in a session once a currency has been selected. Cheers, Jonny Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326785 Share on other sites More sharing options...
jonnystudent Posted March 13, 2012 Author Share Posted March 13, 2012 I've altered my code somewhat as I realised that my previous code was not an array. So my new code is.. if(isset($_GET['c'])) { $currency = $_GET['c']; $currency_array = array( $currency_array [GBP] = 1.00; $currency_array [uSD] = 1.56362 ; $currency_array [EURO]= 1.18894; $currency_array [JPY] = 128.651; $currency_array [AUD] = 1.48711; $currency_array [CHF] = 1.43373; ); if(array($currency, $currency_array)) { $_SESSION['rdt'] = $currency; } } if(!isset($_SESSION['rdt'])) { $_SESSION['rdt'] = 'GBP'; } ?> <span>Currency:</span><a href="http://x/Currency_script.php?c=GBP" class="hi"> £ GBP</a><a href="http://x/Currency_script.php?c=USD"> $ USD</a><a href="http://x/Currency_script.php?c=EURO"> € EURO</a><a href="http://x/Currency_script.php?c=JPY"> ¥ JPY</a><a href="http://x/Currency_script.php?c=AUD"> $ AUD</a><a href="http://x/Currency_script.php?c=CHF"> <span class="currency_sign">Fr</span> CHF</a> Can anyone tell me if this would reload the page with new prices and store it in a session once a currency has been selected. Cheers, Jonny Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326788 Share on other sites More sharing options...
jonnystudent Posted March 13, 2012 Author Share Posted March 13, 2012 Sorry for the double post. Now that I have this script, I'll now need to have a currency base so that I can change the current currency to a global value and then convert that global value to the requested currency. Any ideas will be greatly appreciated. Will I have to connect to Yahoo or Google to get the base rate. How would this be programmed? If anyone could forward me any example, I'll be greatly appreciative. Cheers, Jonah Quote Link to comment https://forums.phpfreaks.com/topic/258640-updating-currency-price-on-page/#findComment-1326798 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.