Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    currency conversion

    Is there a way of checking and displaying the currency conversion for price using php?
    I want to connect to the database get the price in one currency and then check the rate and display the second curreny.

    thanks

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    http://nl2.php.net/manual/nl/function.number-format.php

    think you have to create the rate database yourself

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    oh great!
    thanks

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    You can use PHP to directly access currency exchange rates from Yahoo Finance in real time. The code below will convert $100 US Dollars into Canadian dollars and since currencies are traded almost 24/7 the output always changes. Demo of the script: http://www.rgbpixels.ca/dollar.php

    You would of course have to change the $ticker variable and make some other changes for your needs. Go to Yahoo Finance for the currency symbols.

    PHP Code:
    <?php
    $us_price 
    '100';
    $ticker'CADUSD=X';
    $open fopen("http://quote.yahoo.com/d/quotes.csv?s=$ticker&f=sl1d1t1c1ohgv&e=.csv""r");
    $exchange_rate fread($open2000);
    fclose($open);
    $exchange_rate str_replace("\""""$exchange_rate);
    $exchange_rate explode(","$exchange_rate);
    $ca_price = ($us_price/$exchange_rate[1]);
    $price number_format ($ca_price2);

    echo 
    "$us_price US dollars = \$$price Canadian dollars";
    ?>
    Leonard Whistler

  • #5
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you very much!

  • #6
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    any idea why this might be very slow loading on a page.
    and how i would speed it up?
    is it to do with the server?

    tx

  • #7
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by bunny1 View Post
    any idea why this might be very slow loading on a page.
    and how i would speed it up?
    is it to do with the server?

    tx
    It could be your host that is slow. The script does have to query Yahoo first so there will be a small delay.
    Leonard Whistler

  • #8
    Regular Coder ArcticFox's Avatar
    Join Date
    Jan 2004
    Location
    Vostok Station, AQ
    Posts
    602
    Thanks
    35
    Thanked 3 Times in 3 Posts
    Is there a way to do this for multiple prices without having to insert all that code into each section?

    To look like:

    Price: $50 ($550.66 pesos)
    Price: $74 ($814.98 pesos)
    Price: $99.95 ($1,100.77 pesos)



    Maybe something like
    Code:
    Price: <?php echo "$50 " + "(" + (50 * $mxn_conv) + " pesos)" ?>
    Price: <?php echo "$74 " + "(" + (74 * $mxn_conv) + " pesos)" ?>
    Price: <?php echo "$99.95 " + "(" + (50 * $mxn_conv) + " pesos)" ?>
    {edit} Nightfire's got it: http://www.codingforums.com/showthre...48501&posted=1 {/edit}


    And, is there a way to keep an error from showing if the data happens to be missing? (change "CADUSD=X" to "000000=X" to test)
    Last edited by ArcticFox; 03-22-2007 at 02:40 PM.
    <div> - putting your mind in a box since 1997

  • #9
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by ArcticFox View Post
    And, is there a way to keep an error from showing if the data happens to be missing? (change "CADUSD=X" to "000000=X" to test)
    You are right, data will be missing if Yahoo is too slow and an error will come up, but it doesn't happen often.

    One way around that is to have the script load the information into a local database (MySQL, text file?) on a regular basis using a CRON job or PHP and then your site would get the currency quotes from your database instead of Yahoo. I don't know anything about CRON jobs, but I assume it can handle something like this.
    Leonard Whistler


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •