jozef goetz contribution, 2011 1 credits:

6
Jozef Goetz contribution, 1 Credits: http://www.pixel2life.com/tutorials/php_coding/e_commerce/

Upload: scott-holland

Post on 26-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Jozef Goetz contribution, 2011 1 Credits:

Jozef Goetz contribution, 2011

1

Credits: http://www.pixel2life.com/tutorials/php_coding/e_commerce/

Page 2: Jozef Goetz contribution, 2011 1 Credits:

Jozef Goetz contribution, 2011

Introduction

In this tutorial we will be learning how to make a currency converter that keeps up to date with the exchange rates according to google.

We will start off with a basic converter with a simple front end and expand from there..

The Functions: We will be using 4 predefined functions in this tutorial as follows:

fopen file_get_contents fclose explode

2

Page 3: Jozef Goetz contribution, 2011 1 Credits:

Jozef Goetz contribution, 2011

GUI e-commerce_form.htmlForm To start things off we'll create the forms to pass the data to the php script. I won't explain what the forms do except what the

variables are as they should be quite self explanatory.

�We are only using 5 different currencies to keep things simple.

�view source

print?

<form action="index.php" method="post"><br>

Amount: <input name="amount" type="text"><br><br><br>

03.To: <select name="fromCurrency">

04.<option value="AUD">Australian Dollar (AUD)</option>

05.<option value="CAD">Canadian Dollar (CAD)</option>

06.<option value="EUR">Euro (EUR)</option>

07.<option value="GBP">British Pound (GBP)</option>

08.<option value="USD">United States Dollar (USD)</option>

09.</select><br><br><br>

10.To: <select name="toCurrency">

11.<option value="AUD">Australian Dollar (AUD)</option>

12.<option value="CAD">Canadian Dolar (CAD)</option>

13.<option value="EUR">Euro (EUR)</option>

14.<option value="GBP">British Pound (GBP)</option>

15.<option value="USD">United States Dollar (USD)</option>

16.</select><br><br><br>

17.<input name="submit" value="Submit" type="submit"><br><br>

18.</form>

�The value attribute in each of the option tags are the codes for each currency used by google.

3

Page 4: Jozef Goetz contribution, 2011 1 Credits:

Jozef Goetz contribution, 2011

index_1.php part I�Lets start with our if statement:

<?php

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

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

�1.$amount = $_POST['amount'];

2.$fromCurrency = $_POST['fromCurrency'];

3.$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 ]).

�1.$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

�1.$connect = fopen($filename, "r");

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

�1.$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.

1. fclose($connect); // This closes the connection we just opened.

4

Page 5: Jozef Goetz contribution, 2011 1 Credits:

Jozef Goetz contribution, 2011

index_1.php part II$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.

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

2. 

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.

�1.echo("You entered: " . $conversionData[1]);

2.echo("

3.);

4.echo("That is equal to: " . $conversionData[3]);

5.echo("

6.");

7.echo("The exchange rate is: " . $rate);

};

?>

5

Page 6: Jozef Goetz contribution, 2011 1 Credits:

Jozef Goetz contribution, 2011

Entire index_1.php<?php

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

03.$amount = $_POST['amount'];

04.$fromCurrency = $_POST['fromCurrency'];

05.$toCurrency = $_POST['toCurrency'];

06. 

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

08. 

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

10.$file_contents = file_get_contencts($filename);

11.fclose($connect);

12. 

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

14. 

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

16. 

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

18. 

19.echo("You entered: " . $conversionData[1]);

20.echo("

21.);

22.echo("That is equal to: " . $conversionData[3]);

23.echo("

24.");

25.echo("The exchange rate is: " . $rate);

26.};

27. 

28.?>

6