Jump to content


Photo

currency converter


  • Please log in to reply
9 replies to this topic

#1 fazzfarrell

fazzfarrell
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts

Posted 22 September 2006 - 01:20 PM

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

#2 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 22 September 2006 - 01:26 PM

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.


#3 fazzfarrell

fazzfarrell
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts

Posted 22 September 2006 - 01:44 PM

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

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 22 September 2006 - 01:56 PM

<?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";
?>


#5 fazzfarrell

fazzfarrell
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts

Posted 22 September 2006 - 02:16 PM

Thanks tried this but nothing happens!

#6 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 22 September 2006 - 02:22 PM

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:

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

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?

#7 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 22 September 2006 - 02:24 PM

My code should work. I tested it before posting? Do you get any errors or what happens?

#8 fazzfarrell

fazzfarrell
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts

Posted 22 September 2006 - 02:51 PM

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";
?>

#9 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 22 September 2006 - 02:52 PM

But the dropdown list does show correctly, right?

#10 fazzfarrell

fazzfarrell
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts

Posted 22 September 2006 - 03:00 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users