Publishing System Settings Logout Login Register
Currency Conversion in PHP
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on January 5th, 2010
6048 views
PHP Coding
The Back End

Now we start on the main part of the tutorial.

Here is the code we'll be using, I'll not put any comments in the code but will explain everything soon:

<?php
if (isset($_POST['submit'])) {
$amount = $_POST['amount'];
$fromCurrency = $_POST['fromCurrency'];
$toCurrency = $_POST['toCurrency'];

$filename = "http://www.google.com/ig/calculator?hl=en&q=" . $amount . $fromCurrency . "%3D%3F" . $toCurrency;

$connect = fopen($filename, "r");
$file_contents = file_get_contencts($filename);
fclose($connect);

$conversionData = explode("\"",$file_contents);

$rateData = (explode(" ", $conversionData[3]));

$rate = $rateData[0]/$amount;

echo("You entered: " . $conversionData[1]);
echo("
);
echo("That is equal to: " . $conversionData[3]);
echo("
");
echo("The exchange rate is: " . $rate);
};

?>

Lets start with our if statement:

if (isset($_POST['submit'])) {

This states that if the form has been submitted then the following code will be executed.

$amount = $_POST['amount'];
$fromCurrency = $_POST['fromCurrency'];
$toCurrency = $_POST['toCurrency'];

This gets our variables from the form and places them into PHP variables.

To do this simply add the name of the form element into the quotes in the square brackets([ and ]).

$filename = "http://www.google.com/ig/calculator?hl=en&q=" . $amount . $fromCurrency . "%3D%3F" . $toCurrency;

This is a bit more complicated but we are simply adding our variables to the URL that we find our conversion data.

This will turn out as something like this:

http://www.google.com/ig/calculator?hl=en&q=100USD%3D%3FGBP

$connect = fopen($filename, "r");

This opens a connection to our specified file. The "r" means that it will be read only.

$file_contents = file_get_contents($filename);

This takes the source of the specified file and places it as a string into the variable.

The contents will be something like this:

{lhs: "100 U.S. dollars",rhs: "62.7706986 British pounds",error: "",icc: true}

This needs stripped down to be useful which we will do in a minute.

fclose($connect);

This closes the connection we just opened.

$conversionData = explode("\"",$file_contents);

This splits the string into an array. It will split at every quotation mark("). This means everything up to the first " will be in one part of the array, everything up to the next will be in another part of the array and so on.

The parts we are interested are 1 and 3.

Remember that arrays are 0 based, meaning that the first part of the array is 0 and not 1.

$rateData = (explode(" ", $conversionData[3]));

$rate = $rateData[0]/$amount;

This seperates the string into a number so that it can work out the exchange rate that is being used.

For example it changes 62.7706986 British pounds into 62.7706986.

echo("You entered: " . $conversionData[1]);
echo("
);
echo("That is equal to: " . $conversionData[3]);
echo("
");
echo("The exchange rate is: " . $rate);

Finally we have everything output to the page.

On the next page is a list of all the avaliable currencies that can be added.

Next Page
Pages: 1 2 3 4
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
chironic

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top