Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- HIding Javascript


jd01 - 3:12 am on Jul 14, 2005 (gmt 0)


I don't usually post in here, but I am a little bored today, so I will chime in for a minute:

If you create a file called exchange.php (or anything you want), you can then put the conversion code in a single file that will be used on all pages that need conversions - let me explain...

1st since you have a number of conversions, and will be updating them weekly, we should make the exchange rates easy to get at, so at the top of the exchange.php file you could put this:

<?php
$USexchRate = 1.20379;
$YENexchRate = .78464;

and so on for each currency... then after defining the variables (in the same file) you can create the code that will actually convert the exchange rate:

function getUSD($price) {
global $USexchRate; // get the exchange rate previously defined. function.
$USD = '<b>USD $' . number_format($price * $USexchRate,2) . '</b>';
return $USD;
}

function getYEN($price) {
global $YENexchRate; // get the exchange rate previously defined.
$YEN = '<b>YEN $' . number_format($price * $YENexchRate,2) . '</b>';
return $YEN;
}

and so on for each currnecy/exchange rate you have defined.

So this far we have a file called 'exchange.php' which looks like this (only longer in real life)

<?php
$USexchRate = 1.20379;
$YENexchRate = .78464;

function getUSD($price) {
global $USexchRate; // get the exchange rate we have defined.
$USD = '<b>USD $' . number_format($price * $USexchRate,2) . '</b>';
return $USD;
}

function getYEN($price) {
global $YENexchRate; // get the exchange rate we have defined.
$YEN = '<b>YEN $' . number_format($price * $YENexchRate,2) . '</b>';
return $YEN;
}
?>

Now we have to get the file into the pages we would like to use the exchange rate on. At the top of each page we will tell your server to include it. (This will be the first line of the page, even above the 'doc type' or any html is fine.)

<?php include "exchange.php";?>

This code will cause the file you have just created, 'exchange.php', to be 'part of' the page that is opening, so you can add the above line of code to each page you need an exchange rate on and the 'exchange.php' will be 'included' on each of them.

Finally, you will need to get the numbers back out of the exchange rate functions so we can show the right amounts:

If the price is $42.99 and you would like to convert (getUSD(), does the converting) and display (echo 'value' does the displaying) US dollars, you would use:

<?php echo getUSD(42.99);?>

To convert to YEN, you would use:

<?php echo getYEN(42.99);?>

So the final .html page (probably actually php, but either way it is the main page that will display all the information.) will look something like this:

<?php include "exchange.php";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">

<table> The real price of this item is: $42.99<br>
The USD conversion is: <?php echo getUSD(42.99);?><br>
The YEN conversion is: <?php echo getYEN(42.99);?>
</table>

Hope this helps. Have fun with php.

Justin


Thread source:: http://www.webmasterworld.com/php/9105.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com