Posted on January 5th, 2010
6048 views
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. �
|