To understand this tutorial user must be aware of MVC (Model-View-Controller) concepts & workflows. Converting currency is pretty simple with CodeIgniter in this page we are going to see the following,
- How to write currency converter library
- How to call the library in the controller
- How to interact with Database and Views
Important Update
After getting more visitors to this blog post, recently we have noticed that the demo links were outdated and not working. We have added the same code to the github repository below
Outdated links below will be removed soon
As the first step to create a library, navigate your folder to
application\libraries
Under the above shown folder create a file called “currency_converter.php” which is the library file going to be called in controller later. These library going to have 4 variables in it,
1 2 3 4 5 6 7 8 |
/*Amount in number*/ public $amount; /*Country code from which it has to be converted*/ public $from; /*Country code to which it has to be converted*/ public $to; /*Google Search URL for convertion*/ public $googleUrl = "http://www.google.com/ig/calculator?hl=en&q="; |
Constructor function has to be written to transport the parameters to the library as shown below,
1 2 3 4 5 6 |
public function currency_converter($params) { $this->amount = $params['amount']; $this->from = $params['fromCurrency']; $this->to = $params['toCurrency']; $this->googleUrl = $this->googleUrl."1".$this->from."=?".$this->to; } |
Business logic function has to be created separately to handle each and every request like below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public function getResult(){ $result = file_get_contents($this->googleUrl); /* Convert the above result into Array */ $result = explode('"', $result); /* Right side text*/ $convertedAmount = explode(' ', $result[3]); $conversion = $convertedAmount[0]; $conversion = $conversion * $this->amount; $conversion = round($conversion, 2); //Get text for converted currency $rightText = ucwords(str_replace($convertedAmount[0],"",$result[3])); //Make right hand side string $rightText = $conversion.$rightText; /* Left side text*/ $googleLeft = explode(' ', $result[1]); $fromAmount = $googleLeft[0]; //Get text for converted from currency $fromText = ucwords(str_replace($fromAmount,"",$result[1])); //Make left hand side string $leftText = $this->amount." ".$fromText; return $leftText." = ".$rightText; } |
After creating library navigate to
application\controllers
Under the above shown folder create a file called “currencies.php” which is the controller file. In the controller file have two main functions 1 is for loading the default index and another is for converting & displaying the given input. In the controller you can call the library by using below code,
1 2 3 4 |
$data['fromCurrency'] = 'USD'; $data['toCurrency'] = 'INR'; $data['amount'] = '1'; $this->load->library('currency_converter', $data); |
To show the result from the library,
1 |
$data['conversion_result'] = $this->currency_converter->getResult(); |
Now before displaying the result you need to show the list of countries in the form to convert easily. So need to create a table containing country code and country name. Mysql syntax for creating table is given below,
1 2 3 4 5 6 |
CREATE TABLE IF NOT EXISTS `CountryCode` ( `id` int(11) NOT NULL AUTO_INCREMENT, `CountryCode` char(5) NOT NULL, `CountryName` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1; |
SQl dump for data to be inserted is given in the attachment. Now we have to create a model class for interacting with database and controller.
After creating controller navigate to
application\models
Under the above shown folder create a file called “currenciesmodel.php” which is the model file. In the model file have two main functions 1 is for default constructor and another is for getting the result from the table. For loading the database class in the model use the below code, its because sometime the error will raise like “$db is called on non-object” to avoid this load the database class in the constructor,
1 |
$this->load->database(); |
The second function should fetch the table result from the database function is as shown below,
1 2 3 4 |
function get_country_list(){ $query = $this->db->get('CountryCode'); return $query->result(); } |
In the controller you can call the model with below code snippets,
1 2 |
$this->load->model('CurrenciesModel'); $data['country_details'] = $this->CurrenciesModel->get_country_list(); |
After creating controller navigate to
application\views
Under the above shown folder create a file called “currency_converter.php” which is the view file. In the view file you can access all the data array passed on controller file, the way to include the view file is shown below.
1 2 |
/* whereas $data is an array contains all data, you can see in the attachment */ $this->load->view('currency_converter',$data); |
Once all these setup has been done you can see the output in the browser as below,
Its shows an error -Unable to load the requested class: Currency_converter.How I solve it.