Jump to content

Archived

This topic is now archived and is closed to further replies.

fazzfarrell

currency converter

Recommended Posts

I have built a shopping cart site with php, that works fine but now the customer wants the user to be able to change the currency from GBP to other currencies.

I have looked for solutions but can not find any, can any one point me in the right direction?

thanks

Share this post


Link to post
Share on other sites
you need to have a list of currencies in mysql with exchange rates each should have a batch id as these exchange rates will change. All exchange rates should be against the dollar, you can get these from xe.com.

Then you need to test the currency that the user has and pull the latest batch id currency from the currency that they want to convert into. Then use this to calcuate your new currency and x the amount by this value. You will need to do some formating on the result after as you may well have the odd amount of the point after.

Share this post


Link to post
Share on other sites
Thanks,

I have entered this code:

<select name="curr" style="width:150px">
                  <option value="USD" <?php ($HTTP_SESSION_VARS["icJag"]->DisplayCurrency == "USD") ? echo "SELECTED": echo ""; ?>>US Dollar (USD)</option>
                  <option value="EUR" <?php ($HTTP_SESSION_VARS["icJag"]->DisplayCurrency == "EUR") ? echo "SELECTED": echo ""; ?>>Euro (EUR)</option>
                  <option value="GBP" <?php ($HTTP_SESSION_VARS["icJag"]->DisplayCurrency == "GBP") ? echo "SELECTED": echo ""; ?>>British Pound (GBP)</option>
                </select>

Which should convert the currency symbol but I am getting this error: Parse error: parse error, unexpected T_ECHO in /home/sites/jaguar.com/public_html/results2.php on line 448


I can't see why it is doing this? any ideas

Share this post


Link to post
Share on other sites
[code]<?php
$currencies = array(
'usd' => "US Dollar",
'eur' => "Euro",
'gbp' => "British Pound",
);

echo "<select name='curr' style='width:150px'>\n";
foreach($currencies as $short => $long)
{
$short = strtoupper($short);
$selected = $_SESSION['icJag']->DisplayCurrency == $short ? "selected='selected'" : null;
echo "\t<option value='{$short}'{$selected}>{$long} ({$short})</option>\n";
}
echo "</select>\n";
?>
[/code]

Share this post


Link to post
Share on other sites
in order to use the ternary operator as you are in your original code (Daniel0's should have fixed that problem), you need to start with a command as far as i know.  if you're going to start with an if() statement, why bother using the ternary?

in order to correct your original code, you'd need to change your lines to this:

[code]<option value="USD" <?php echo ($HTTP_SESSION_VARS["icJag"]->DisplayCurrency == "USD") ? 'SELECTED' : ''; ?>>US Dollar (USD)</option>[/code]

that being said, Daniel0's code is much cleaner, and i'd suggest using that.  what do you mean by "nothing happens"?  no select box, an empty select box, shoddy HTML output, or what?

Share this post


Link to post
Share on other sites
My code should work. I tested it before posting? Do you get any errors or what happens?

Share this post


Link to post
Share on other sites
Im lost now!

this is my full page code, but when I select basicly nothing happens on the page other than price totals revert to '0.00'

I think i am missing something!


<?php
// IntelliCART MX - Country Code Selector
if ($HTTP_POST_VARS["curr"] != $HTTP_SESSION_VARS["icJag"]->DisplayCurrency) {
$HTTP_SESSION_VARS["icJag"]->DisplayCurrency = $HTTP_POST_VARS["curr"];
}
?>

then for the drop downs

<?php
$currencies = array(
'usd' => "US Dollar",
'eur' => "Euro",
'gbp' => "British Pound",
);

echo "<select name='curr' style='width:150px'>\n";
foreach($currencies as $short => $long)
{
$short = strtoupper($short);
$selected = $_SESSION['icJag']->DisplayCurrency == $short ? "selected='selected'" : null;
echo "\t<option value='{$short}'{$selected}>{$long} ({$short})</option>\n";
}
echo "</select>\n";
?>

Share this post


Link to post
Share on other sites
Yes it now displays correctly great!

But I can not get the currency on screen to change to $ etc. What I am trying to do is

I have the GBP prices in a database they are the default prices on the site

I want a drop down wih the other currencies that can be chosen.

When ou select say 'USD' the price and '£' change to the dollar conversion through to payment.

Does that make sense?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.