Question
This lab is intended to give you introductory experience in writing your first real PHP script, a key component to your web application. Your goal
This lab is intended to give you introductory experience in writing your first real PHP script, a key component to your web application. Your goal in this exercise is to extend the work you started in Lab #2 where you created an HTML page which allows a user to obtain foreign exchange (socalled F/X) rates. F/X rates specify the translation or conversion between one currency such as the US Dollar (USD) and another, for example, the Great Britain Pound (GBP), the Euro (EUR) or the Canadian Dollar (CAD). The screenshot below shows you what I was expecting in the previous lab. As you may recall, this file was to be named fxCalc.html.[1]
At this time you will create its counterpart, fxCalc.php. This PHP script works together with fxCalc.html which still remains the entry point for the application[2]. fxCalc.php works in conjunction with static methods from the class FxDataModel which you must also write. The idea of a PHP script working with objects is very common in web development. Eventually, we will learn about MVC or the Model-View-Controller design pattern and you will see where PHP scripts and objects fit in that paradigm. For now, I will describe how you will write and implement these components. Lets start with the data model.
FxDataModel should be a class in the file FxDataModel.php. This file is to be placed in the same folder as fxCalc.html and fxCalc.php. Characteristics of the class are as follows:
a private static array containing the currency codes from the last lab in ascending order.
a private static two-dimensional array containing the F/X rates between the currencies. Use the following values in the following order:
1.0 | 0.624514066 | 0.588714763 | 0.810307 |
1.601244959 | 1.0 | 0.942676548 | 1.2975 |
1.698615463 | 1.060809248 | 1.0 | 1.3764 |
1.234100162 | 0.772200772 | 0.726532984 | 1.0 |
For example, since CAD is the zeroth currency (note that PHP arrays start at index 0 and not 1) and USD is the last (index 3 in this case), then the element in row 0 / column 3 represents the CAD to USD exchange rate and 1 CAD = 0.810307 USD. On the other hand, if we want the USD to CAD exchange rate, it is the element in row 3 / column 0 which tells is that 1 USD = 1.234100162 CAD. This same approach can be used for any two currencies. Note that the exchange rate between any currency and itself is 1.0.
it contains a public static method named getFxCurrencies that returns the array of currency codes.
it contains a public static method named getFxRate that returns the exchange rate between the currency codes passed in as the first and second arguments. The first argument is the source or in currency, whereas the second is the destination or out currency.
feel free to embellish on the class with other components ( constants, methods, etc. ) that could be useful in fxCalc.php or elsewhere in your web application. However, as a bare minimum, you must include all the elements I have described above.
With the data model behind us, we move now to fxCalc.php which provides the view component of the web application. fxCalc.php should have the following properties from top to bottom:
a require_once statement that includes the file FxDataModel.php.
code following the above that does the work of computing the amount of money into which the input amount plus the source and destination currency translates. The data model method, getFxRate, provides everything you need to calculate this since it provides the F/X rate between two currencies. Since you have named your currency and amount form variables in the last lab, you can now use the PHP intrinsic $_POST array to get those parameters by their names. Chapter 7 of the text discusses this thoroughly. While it is fine for the currency codes to be strings, if the user enters a non-numeric value for the source amount, a conversion is not possible. You need to test for this eventuality as discussed in Chapter 2. Should the user enter a nonnumeric value for the input amount, your code should essentially reset the form so that the in and out amounts and currencies return to their defaults. Should a conversion be possible, populate the out amount text field with that value. Dont worry about decimal formatting or currency signs unless you are so inclined to embellish upon the PHP look and functionality. The same is true for the reset button on fxCalc.php.
the HTML that appears in fxCalc.php following the scriptlet code discussed above is very similar to what already exists in fxCalc.html. There will, however, be a few modifications which will make the application dynamic. One such case is that you will not hard code the currency codes as you did previously in the dropdown list boxes. You will break out of HTML with a PHP scriplet that iterates through the currency codes contained in the array of codes discussed above. You do this at the point in the HTML where you would populate the HTML option tags. You can dynamically create the drop-down list box using a loop. The loop variable (whatever you care to call it, but currency is as likely a candidate as anything else), can be used in a PHP expression which literally echos out the variable in the value attribute of the option tag. You will mix HTML and scriptlets as shown in your text. You will do so with a combination of loops and branching. HTML code in your loop is repeated as if it was hard-coded multiple times. You must also use if/else blocks to selectively print the HTML of your choice. For example, imagine a user selects USD and GBP. As your PHP scriptlet is iterating through the currency codes array to build the drop-down list box, you can condition the code to add the attribute selected to the option tag corresponding to the user-selected in and out currencies. Doing this will allow the user-provided currencies (both in and out) to be the selected ones. This is shown in the screen shot below in which the user tries to find the conversion between 25 USD and GBP. In addition, the input amount and calculated output amount are scriptlet-defined local variables that will appear as expressions in the value attribute of the in and out amount text boxes.
[1] For further details I urge you to re-read Lab02.doc.
[2] There are obviously limitations in using fxCalc.html not the least of which is the fact that it hard-codes the currency codes. We will be addressing this in the next lab.
Money Banks F/X Calculator CAD CAD EUR GBP USD Money Banks F/X Calculator CAD CAD EUR GBP USDStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started