SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Automatic Currency Conversions ?

    Hi all,

    Just trying to do 4 currency conversions on this page below :

    http://www.130605.com/currency_rates

    In some way using :

    http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

    The code for the page :

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Language" content="en-gb">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Currency</title>
    </head>
    <body bgcolor="#FFFFFF">
    <br>
    <br>
    <table cellpadding="0" cellspacing="0" width="660">
    <tr>
    <td width="50">&nbsp;</td>
    <td>
    1 British Pound &nbsp; = &nbsp;&nbsp;&nbsp;&nbsp; Euros
    <br>
    <br>
    1 Euro &nbsp; = &nbsp;&nbsp;&nbsp;&nbsp; British Pounds
    <br>
    <br>
    <br>
    1 US Dollar &nbsp; = &nbsp;&nbsp;&nbsp;&nbsp; Euros
    <br>
    <br>
    1 Euro &nbsp; = &nbsp;&nbsp;&nbsp;&nbsp; US Dollars
    <br>
    &nbsp;
    </td>
    </tr>
    </table>
    <br>
    <br>
    <br>
    <br>
    </body>
    </html>

    As you can imagine, the 4 amounts on the left would always be the same, with the 4 amounts on the right changing.

    I know how to set permissions and can set cronjobs, but just need a bit of a hand on making the 4 conversions happen automatically please.

    Any help much appreciated.

    Dez.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    hope this helps:

    PHP Code:
    <?php

    $rates 
    file_get_contents('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');

    $dol_rate_start strpos($rates"USD' rate='") + 11;
    $dol_rate_end strpos($rates"'/>"$dol_rate_start);
    $dol_rate_length $dol_rate_end $dol_rate_start;
    $dollar_rate substr($rates$dol_rate_start$dol_rate_length);

    $pnd_rate_start strpos($rates"GBP' rate='") + 11;
    $pnd_rate_end strpos($rates"'/>"$pnd_rate_start);
    $pnd_rate_length $pnd_rate_end $pnd_rate_start;
    $pound_rate substr($rates$pnd_rate_start$pnd_rate_length);

    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Language" content="en-gb">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Currency</title>
    </head>
    <body bgcolor="#FFFFFF">
    <br />
    <br />
    <table cellpadding="0" cellspacing="0" width="660">
        <tr>
            <td width="50">&nbsp;</td>
            <td>1 British Pound &nbsp; = <?php echo round($pound_rate2?> Euros
            <br /><br />
            1 Euro &nbsp; = <?php echo round(1/$pound_rate2?> British Pounds
            <br />
            <br />
            <br />
            1 US Dollar &nbsp; = <?php echo round(1/$dollar_rate2?> Euros
            <br />
            <br />
            1 Euro &nbsp; = <?php echo round($dollar_rate2?> US Dollars
            <br />&nbsp;
            </td>
        </tr>
    </table>
    <br />
    <br />
    <br />
    <br />
    </body>
    </html>
    Sincerely,
    Joe Belmaati
    Copenhagen Denmark

  3. #3
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Many thanks for that Joe - it's very much appreciated.

    I'll get to work on that.

    Do I have to set any cronjobs at all please ?

    Dez.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, the script works in real time. When the ECB changes the rates your page will refletct it.
    Sincerely,
    Joe Belmaati
    Copenhagen Denmark

  5. #5
    PEACE WILL WIN abalfazl's Avatar
    Join Date
    Feb 2005
    Location
    Beyond the seas there is a town
    Posts
    711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello my firend

    You can do that by XMLhttp request,

    Or realod that page,You can modify the document.location property to relaod the page.

    GOOD LUCK!
    I shall build a boat,I shall cast it in the water,
    I shall sail away from this strange earth,
    Where no one awaken the heroes in the wood of love

  6. #6
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Hi Joe and Abalfazl - many thanks to you both - it is very much appreciated.

    The code that Joe kindly submitted works perfectly - mind you, with the level of expertise on these forums, it was never in doubt really

    2 Things if you can please :

    1. It isn't so critical for the dates to be bang up to date - could anyone help me to setup a cronjob, so that it just updates twice a day please.

    2. Is it possible to have 3 digits after the full stop, ie instead of 1.47, it would say 1.476 for example please.

    Any help much appreciated.

    Dez.
    Last edited by Dez; Aug 21, 2006 at 07:44.

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi Dez,

    Firstly, please accept my apologies for my late reply, I read your PM but could not reply immediately, and later I forgot.

    As far as I remember the script you were using was based on a text file which depended on a cron job. Do you mean that the cron job don't work?
    Saul

  8. #8
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Hi php_daemon,

    No problem about the pm - I'm just glad for the help you give.

    The cronjob you helped with on the other project works fine, but this one is a different one - would you be able to help on this one at all please ?

    All the best.

    Dez.

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see.

    OK, using Joe Belmaati's code, place this code to a separate php file:
    PHP Code:
     <?php

    $rates 
    file_get_contents('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');

    $dol_rate_start strpos($rates"USD' rate='") + 11;
    $dol_rate_end strpos($rates"'/>"$dol_rate_start);
    $dol_rate_length $dol_rate_end $dol_rate_start;
    $dollar_rate substr($rates$dol_rate_start$dol_rate_length);

    $pnd_rate_start strpos($rates"GBP' rate='") + 11;
    $pnd_rate_end strpos($rates"'/>"$pnd_rate_start);
    $pnd_rate_length $pnd_rate_end $pnd_rate_start;
    $pound_rate substr($rates$pnd_rate_start$pnd_rate_length);

    $ff=fopen("currency.txt","w");
    fwrite($ff,$dollar_rate."\n".$pound_rate);
    fclose($ff);

    ?>
    Set 0666 permissions for currency.txt and set this php file as a cron job. It will update currency.txt with new rates for the period you set the corn job to.

    Now back to your page:
    PHP Code:
    <?php
    $rates
    =file("currency.txt");
    $dollar_rate=$rates[0];
    $pound_rate=$rates[1];
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head> 
    <meta http-equiv="Content-Language" content="en-gb"> 
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
    <title>Currency</title> 
    </head> 
    <body bgcolor="#FFFFFF"> 
    <br /> 
    <br /> 
    <table cellpadding="0" cellspacing="0" width="660"> 
        <tr> 
            <td width="50">&nbsp;</td> 
            <td>1 British Pound &nbsp; = <?php echo round($pound_rate2?> Euros 
            <br /><br /> 
            1 Euro &nbsp; = <?php echo round(1/$pound_rate2?> British Pounds 
            <br /> 
            <br /> 
            <br /> 
            1 US Dollar &nbsp; = <?php echo round(1/$dollar_rate2?> Euros 
            <br /> 
            <br /> 
            1 Euro &nbsp; = <?php echo round($dollar_rate2?> US Dollars 
            <br />&nbsp; 
            </td> 
        </tr> 
    </table> 
    <br /> 
    <br /> 
    <br /> 
    <br /> 
    </body> 
    </html>
    Saul

  10. #10
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by php_daemon
    Ah, I see.

    OK, using Joe Belmaati's code, place this code to a separate php file:
    PHP Code:
     <?php

    $rates 
    file_get_contents('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');

    $dol_rate_start strpos($rates"USD' rate='") + 11;
    $dol_rate_end strpos($rates"'/>"$dol_rate_start);
    $dol_rate_length $dol_rate_end $dol_rate_start;
    $dollar_rate substr($rates$dol_rate_start$dol_rate_length);

    $pnd_rate_start strpos($rates"GBP' rate='") + 11;
    $pnd_rate_end strpos($rates"'/>"$pnd_rate_start);
    $pnd_rate_length $pnd_rate_end $pnd_rate_start;
    $pound_rate substr($rates$pnd_rate_start$pnd_rate_length);

    $ff=fopen("currency.txt","w");
    fwrite($ff,$dollar_rate."\n".$pound_rate);
    fclose($ff);

    ?>
    Set 0666 permissions for currency.txt and set this php file as a cron job. It will update currency.txt with new rates for the period you set the corn job to.

    Many thanks for the help - it's appreciated.

    I've called the file above get_rates.php and set it up as a cronjob which initiated a little while ago, I've set the permissions on the index page and get_rates.php to 777, but getting the errors on this page below ?

    http://www.130605.com/currency_rates/

    It's probably something really small thats going wrong, any ideas at all please ?

    Dez.

  11. #11
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First of all please change back the permissions for the index page and get_rates.php. It's not necessary to have 777 on them and is very insecure.

    Then create a file currency.txt and set 666 permissions for it. That should solve the problem.
    Saul

  12. #12
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Many, many thanks - your help is always appreciated my friend.

    As mentioned earlier, do you think it would be possible to get it to have 3 numbers after the full stop at all ? So that for example, instead of it just saying 1.47, it might say something like 1.476.

    Any help much appreciated.

    Dez.

  13. #13
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On this bit.

    Code:
     <tr> 
            <td width="50">&nbsp;</td> 
            <td>1 British Pound &nbsp; = <?php echo round($pound_rate, 2) ?> Euros 
            <br /><br /> 
            1 Euro &nbsp; = <?php echo round(1/$pound_rate, 2) ?> British Pounds 
            <br /> 
            <br /> 
            <br /> 
            1 US Dollar &nbsp; = <?php echo round(1/$dollar_rate, 2) ?> Euros 
            <br /> 
            <br /> 
            1 Euro &nbsp; = <?php echo round($dollar_rate, 2) ?> US Dollars 
            <br />&nbsp; 
            </td> 
        </tr>
    Change where you see the 2 to 3 in the round functions.
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  14. #14
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Many thanks for that themightystephen - problem solved.

    To finish up on this little challenge - Many, many thanks to Joe, php_Daemon and themightystephen - all part of a great team - The SitePoint Team - Unbeatable !

    Much appreciated.

    Dez.

  15. #15
    SitePoint Guru themightystephen's Avatar
    Join Date
    Mar 2005
    Location
    England
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't forget clamcrusher - hes awesome with php (even though he wasn't in this thread).
    Get your heelys now at flywalk.co.uk - But what are heelys?
    Heelys are simply shoes with wheels in the heels!

    Flywalk.co.uk - The UK Heelys Retailer

  16. #16
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by themightystephen
    Don't forget clamcrusher - hes awesome with php (even though he wasn't in this thread).
    Yes, indeed, and many others, it is not possible to list them all. Their contributions solved many problems and will solve even more in the future. Long live SitePoint!
    Saul

  17. #17
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Yep, I haven't come across Clamcrusher yet, but I'm sure I will

    Dez.

  18. #18
    SitePoint Wizard
    Join Date
    Jun 2005
    Posts
    1,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all,

    I think I've gone wrong somewhere, it seems that the rates will only update, if I upload currency.txt ? ;-(

    It's now important that one domain gets it's info from another one, so that :

    As an example, in www.123-aa.com/currency/index.php you display the currency

    and that page gets its calculations from www.456-bb.com/currency/currency.txt

    which gets its calculations from www.456-bb.com/get_rates.php


    Have I got it correct that the cron job :

    php -q /home/mine/www/currency/get_rates.php > /dev/null

    needs to be on the www.456-bb.com domain (Which I would prefer really)


    Also, please, in the context of the above text and excellent help above from the others here, could anyone please fill in/confirm what the chmod's for the bits below should be ?

    currency.txt - 666 ? The folder that contains currency.txt - ?

    get_rates.php - ? The folder that contains get_rates.php - ?

    index.php - ? The folder that contains index.php - ?

    I haven't come on here lightly, as I like to leave you people alone as much as possible, but I've been on this for a while and could do with a little more help.

    Any help appreciated.

    All the best.

    Dez.
    Last edited by Dez; Sep 22, 2006 at 23:56.


Bookmarks

Posting Permissions

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