Created
December 20, 2012 01:36
-
-
Save tayler/4342309 to your computer and use it in GitHub Desktop.
This is a simple currency converter. The list of allowed currencies and exchange rates are stored in a single database table with the following fields: currency_id (int), currency_code (3 letter string), exchange_rate (float, stored as the rate from US dollars to the given currency)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ========================================================================== | |
Model | |
============================================================================= */ | |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Exchange_calculator_model extends CI_Model { | |
public function __construct() { | |
$this->load->database(); | |
} | |
public function get_exchg_rate($currency = NULL) { | |
$error = "That code does not exist or is in an incorrect format."; | |
// check that input contains only alphabetic characters | |
if (ctype_alpha($currency) && strlen($clean_currency) == 3) { | |
// use Active Record to run query | |
$this->db->select('exchange_rate'); | |
$this->db->from('exchange_rates'); | |
$this->db->where('currency_code', $clean_currency); | |
$query = $this->db->get(); | |
// check that we got results | |
if ($query->num_rows() > 0) { | |
foreach ($query->result() as $row) { | |
$exchg_rate = $row->rate; | |
} | |
} else { | |
return $error; | |
} | |
return $exchg_rate; | |
} else { | |
return $error; | |
} | |
} | |
} | |
?> | |
/* ========================================================================== | |
Controller | |
============================================================================= */ | |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
class Exchange_calculator extends CI_Controller { | |
public function __construct() { | |
$this->load->model('exchange_calculator_model'); | |
} | |
// checks to make sure the returned value from the model is not an error | |
private function return_error($exchg_rate) { | |
if (is_float($exchg_rate) || is_int($exchg_rate)) { | |
return; | |
} else { | |
// in this case, $exchg_rate will be the error from the model, and it is sent to the user | |
echo $exchg_rate; | |
} | |
} | |
public function convert_currency($currencyFrom, $currencyTo, $amount) { | |
$exchg_rate; | |
$converted; | |
$from_exchg_rate; | |
$to_exchg_rate; | |
$usd; | |
// if exchanging from USD, get exchange rate and multiply by amount | |
if ($currencyFrom == “USD”) { | |
$exchg_rate = $this->exchange_calculator_model->get_exchg_rate($currencyTo); | |
$this->return_error($exchg_rate); | |
$converted = $amount * $exchg_rate; | |
} elseif ($currencyTo == “USD”) { | |
// if exchanging to USD, get exchange rate and divide amount by it | |
$exchg_rate = $this->exchange_calculator_model->get_exchg_rate($currencyFrom); | |
$this->return_error($exchg_rate); | |
$converted = $amount / $exchg_rate; | |
} else { | |
// if neither currency is USD, convert to USD and multiply by exchange rate since we only have exchange rates to USD | |
$from_exchg_rate = $this->exchange_calculator_model->get_exchg_rate($currencyFrom); | |
$this->return_error($from_exchg_rate); | |
$to_exchg_rate = $this->exchange_calculator_model->get_exchg_rate($currencyTo); | |
$this->return_error($to_exchg_rate); | |
$usd = $amount / $from_exchg_rate; | |
$converted = $usd * $to_exchg_rate; | |
} | |
return $converted; | |
} | |
} | |
?> | |
/* | |
* ExchangeCalculator.convert_currency(“USD”, “PHP”, 100) should return 4312 | |
* ExchangeCalculator.convert_currency(“PHP”, “USD”, 100) should return 2.32 | |
* | |
* If LEU has hypothetical exchange rate to USD of 10: | |
* ExchangeCalculator.convert_currency(“PHP”, “LEU”, 100) should return 23.19 | |
* ExchangeCalculator.convert_currency(“LEU”, “PHP”, 100) should return 431.23 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment