Date Posted:02-06-2017
In this post  we will explain create new library in codeigniter for currency conversion,symbol
I assume that you are configure or setup the codeigniter

Step 1:Create the mysql table currency,use following code

-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jun 06, 2017 at 06:25 AM
-- Server version: 5.5.27
-- PHP Version: 5.4.7

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `codeigniter`
--

-- --------------------------------------------------------

--
-- Table structure for table `currency`
--

CREATE TABLE IF NOT EXISTS `currency` (
  `currency_id` int(11) NOT NULL AUTO_INCREMENT,
  `currency_name` varchar(64) DEFAULT NULL,
  `currency_code` char(3) DEFAULT NULL,
  `symbol` varchar(150) NOT NULL,
  `hex` varchar(150) NOT NULL,
  `status` int(11) DEFAULT NULL,
  PRIMARY KEY (`currency_id`),
  KEY `idx_currency_name` (`currency_name`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=168 ;

--
-- Dumping data for table `currency`
--

INSERT INTO `currency` (`currency_id`, `currency_name`, `currency_code`, `symbol`, `hex`, `status`) VALUES
(1, 'Andorran Peseta', 'ADP', '', '', NULL),
(2, 'United Arab Emirates Dirham', 'AED', 'د.إ', 'د.إ', NULL),
(3, 'Afghanistan Afghani', 'AFA', '', '', NULL),
(4, 'Albanian Lek', 'ALL', '', '', NULL),
(5, 'Netherlands Antillian Guilder', 'ANG', 'ƒ', 'ƒ', NULL),
(6, 'Angolan Kwanza', 'AOK', '', '', NULL),
(7, 'Argentine Peso', 'ARS', '$', '$', NULL),
(9, 'Australian Dollar', 'AUD', 'A$', 'A$', NULL),
(10, 'Aruban Florin', 'AWG', '', '', NULL),
(11, 'Barbados Dollar', 'BBD', '', '', NULL),
(12, 'Bangladeshi Taka', 'BDT', '', '', NULL),
(14, 'Bulgarian Lev', 'BGN', '', '', NULL),
(15, 'Bahraini Dinar', 'BHD', '', '', NULL),
(16, 'Burundi Franc', 'BIF', '', '', NULL),
(17, 'Bermudian Dollar', 'BMD', '', '', NULL),
(18, 'Brunei Dollar', 'BND', '', '', NULL),
(19, 'Bolivian Boliviano', 'BOB', '', '', NULL),
(20, 'Brazilian Real', 'BRL', 'R$', 'R$', NULL),
(21, 'Bahamian Dollar', 'BSD', 'B$', 'B$', NULL),
(22, 'Bhutan Ngultrum', 'BTN', '', '', NULL),
(23, 'Burma Kyat', 'BUK', '', '', NULL),
(24, 'Botswanian Pula', 'BWP', '', '', NULL),
(25, 'Belize Dollar', 'BZD', '', '', NULL),
(26, 'Canadian Dollar', 'CAD', '$', '$', NULL),
(27, 'Swiss Franc', 'CHF', 'CHF', 'CHF', NULL),
(28, 'Chilean Unidades de Fomento', 'CLF', '', '', NULL),
(29, 'Chilean Peso', 'CLP', '$', '$', NULL),
(30, 'Yuan (Chinese) Renminbi', 'CNY', '¥', '¥', NULL),
(31, 'Colombian Peso', 'COP', '$', '$', NULL),
(32, 'Costa Rican Colon', 'CRC', '', '', NULL),
(33, 'Czech Republic Koruna', 'CZK', 'Kč', 'Kč', NULL),
(34, 'Cuban Peso', 'CUP', '', '', NULL),
(35, 'Cape Verde Escudo', 'CVE', '', '', NULL),
(36, 'Cyprus Pound', 'CYP', '', '', NULL),
(40, 'Danish Krone', 'DKK', 'kr', 'kr', NULL),
(41, 'Dominican Peso', 'DOP', '', '', NULL),
(42, 'Algerian Dinar', 'DZD', '', '', NULL),
(43, 'Ecuador Sucre', 'ECS', '', '', NULL),
(44, 'Egyptian Pound', 'EGP', '', '', NULL),
(45, 'Estonian Kroon (EEK)', 'EEK', '', '', NULL),
(46, 'Ethiopian Birr', 'ETB', '', '', NULL),
(47, 'Euro', 'EUR', '€', '€', NULL),
(49, 'Fiji Dollar', 'FJD', 'FJ$', 'FJ$', NULL),
(50, 'Falkland Islands Pound', 'FKP', '', '', NULL),
(52, 'British Pound', 'GBP', '£', '£', NULL),
(53, 'Ghanaian Cedi', 'GHC', '', '', NULL),
(54, 'Gibraltar Pound', 'GIP', '', '', NULL),
(55, 'Gambian Dalasi', 'GMD', '', '', NULL),
(56, 'Guinea Franc', 'GNF', '', '', NULL),
(58, 'Guatemalan Quetzal', 'GTQ', 'Q', 'Q', NULL),
(59, 'Guinea-Bissau Peso', 'GWP', '', '', NULL),
(60, 'Guyanan Dollar', 'GYD', '', '', NULL),
(61, 'Hong Kong Dollar', 'HKD', '$', '$', NULL),
(62, 'Honduran Lempira', 'HNL', 'L', 'L', NULL),
(63, 'Haitian Gourde', 'HTG', '', '', NULL),
(64, 'Hungarian Forint', 'HUF', 'Ft', 'Ft', NULL),
(65, 'Indonesian Rupiah', 'IDR', 'Rp', 'Rp', NULL),
(66, 'Irish Punt', 'IEP', '', '', NULL),
(67, 'Israeli Shekel', 'ILS', '₪', '₪', NULL),
(68, 'Indian Rupee', 'INR', '₹', '₹', 1),
(69, 'Iraqi Dinar', 'IQD', '', '', NULL),
(70, 'Iranian Rial', 'IRR', '', '', NULL),
(73, 'Jamaican Dollar', 'JMD', 'J$', 'J$', NULL),
(74, 'Jordanian Dinar', 'JOD', '', '', NULL),
(75, 'Japanese Yen', 'JPY', '¥', '¥', NULL),
(76, 'Kenyan Schilling', 'KES', '', '', NULL),
(77, 'Kampuchean (Cambodian) Riel', 'KHR', '', '', NULL),
(78, 'Comoros Franc', 'KMF', '', '', NULL),
(79, 'North Korean Won', 'KPW', '', '', NULL),
(80, '(South) Korean Won', 'KRW', '₩', '₩', NULL),
(81, 'Kuwaiti Dinar', 'KWD', '', '', NULL),
(82, 'Cayman Islands Dollar', 'KYD', '', '', NULL),
(83, 'Lao Kip', 'LAK', '', '', NULL),
(84, 'Lebanese Pound', 'LBP', '', '', NULL),
(85, 'Sri Lanka Rupee', 'LKR', '₨', '₨', NULL),
(86, 'Liberian Dollar', 'LRD', '', '', NULL),
(87, 'Lesotho Loti', 'LSL', '', '', NULL),
(89, 'Libyan Dinar', 'LYD', '', '', NULL),
(90, 'Moroccan Dirham', 'MAD', '.د.م', '.د.م', NULL),
(91, 'Malagasy Franc', 'MGF', '', '', NULL),
(92, 'Mongolian Tugrik', 'MNT', '', '', NULL),
(93, 'Macau Pataca', 'MOP', '', '', NULL),
(94, 'Mauritanian Ouguiya', 'MRO', '', '', NULL),
(95, 'Maltese Lira', 'MTL', '', '', NULL),
(96, 'Mauritius Rupee', 'MUR', '', '', NULL),
(97, 'Maldive Rufiyaa', 'MVR', '', '', NULL),
(98, 'Malawi Kwacha', 'MWK', '', '', NULL),
(99, 'Mexican Peso', 'MXP', '', '', NULL),
(100, 'Malaysian Ringgit', 'MYR', 'RM', 'RM', NULL),
(101, 'Mozambique Metical', 'MZM', '', '', NULL),
(102, 'Namibian Dollar', 'NAD', '', '', NULL),
(103, 'Nigerian Naira', 'NGN', '', '', NULL),
(104, 'Nicaraguan Cordoba', 'NIO', '', '', NULL),
(105, 'Norwegian Kroner', 'NOK', 'kr', 'kr', NULL),
(106, 'Nepalese Rupee', 'NPR', '', '', NULL),
(107, 'New Zealand Dollar', 'NZD', '$', '$', NULL),
(108, 'Omani Rial', 'OMR', '', '', NULL),
(109, 'Panamanian Balboa', 'PAB', 'B/.', 'B/.', NULL),
(110, 'Peruvian Nuevo Sol', 'PEN', 'S/.', 'S/.', NULL),
(111, 'Papua New Guinea Kina', 'PGK', '', '', NULL),
(112, 'Philippine Peso', 'PHP', '₱', '₱', NULL),
(113, 'Pakistan Rupee', 'PKR', '₨', '₨', NULL),
(114, 'Polish Zloty', 'PLN', 'zł', 'zł', NULL),
(116, 'Paraguay Guarani', 'PYG', '', '', NULL),
(117, 'Qatari Rial', 'QAR', '', '', NULL),
(118, 'Romanian Leu', 'RON', 'lei', 'lei', NULL),
(119, 'Rwanda Franc', 'RWF', '', '', NULL),
(120, 'Saudi Arabian Riyal', 'SAR', '', '', NULL),
(121, 'Solomon Islands Dollar', 'SBD', '', '', NULL),
(122, 'Seychelles Rupee', 'SCR', '', '', NULL),
(123, 'Sudanese Pound', 'SDP', '', '', NULL),
(124, 'Swedish Krona', 'SEK', 'kr', 'kr', NULL),
(125, 'Singapore Dollar', 'SGD', 'S$', 'S$', NULL),
(126, 'St. Helena Pound', 'SHP', '', '', NULL),
(127, 'Sierra Leone Leone', 'SLL', '', '', NULL),
(128, 'Somali Schilling', 'SOS', '', '', NULL),
(129, 'Suriname Guilder', 'SRG', '', '', NULL),
(130, 'Sao Tome and Principe Dobra', 'STD', '', '', NULL),
(131, 'Russian Ruble', 'RUB', 'руб', 'руб', NULL),
(132, 'El Salvador Colon', 'SVC', '', '', NULL),
(133, 'Syrian Potmd', 'SYP', '', '', NULL),
(134, 'Swaziland Lilangeni', 'SZL', '', '', NULL),
(135, 'Thai Baht', 'THB', '฿', '฿', NULL),
(136, 'Tunisian Dinar', 'TND', 'DT', 'DT', NULL),
(137, 'Tongan Paanga', 'TOP', '', '', NULL),
(138, 'East Timor Escudo', 'TPE', '', '', NULL),
(139, 'Turkish Lira', 'TRY', 'TL', 'TL', NULL),
(140, 'Trinidad and Tobago Dollar', 'TTD', '$', '$', NULL),
(141, 'Taiwan Dollar', 'TWD', 'NT$', 'NT$', NULL),
(142, 'Tanzanian Schilling', 'TZS', '', '', NULL),
(143, 'Uganda Shilling', 'UGX', '', '', NULL),
(144, 'US Dollar', 'USD', '$', '$', NULL),
(145, 'Uruguayan Peso', 'UYU', '', '', NULL),
(146, 'Venezualan Bolivar', 'VEF', 'Bs', 'Bs', NULL),
(147, 'Vietnamese Dong', 'VND', '₫', '₫', NULL),
(148, 'Vanuatu Vatu', 'VUV', '', '', NULL),
(149, 'Samoan Tala', 'WST', '', '', NULL),
(150, 'Communauté Financière Africaine BEAC, Francs', 'XAF', 'FCFA', 'FCFA', NULL),
(151, 'Silver, Ounces', 'XAG', '', '', NULL),
(152, 'Gold, Ounces', 'XAU', '', '', NULL),
(153, 'East Caribbean Dollar', 'XCD', '$', '$', NULL),
(154, 'International Monetary Fund (IMF) Special Drawing Rights', 'XDR', '', '', NULL),
(155, 'Communauté Financière Africaine BCEAO - Francs', 'XOF', '', '', NULL),
(156, 'Palladium Ounces', 'XPD', '', '', NULL),
(157, 'Comptoirs Français du Pacifique Francs', 'XPF', 'F', 'F', NULL),
(158, 'Platinum, Ounces', 'XPT', '', '', NULL),
(159, 'Democratic Yemeni Dinar', 'YDD', '', '', NULL),
(160, 'Yemeni Rial', 'YER', '', '', NULL),
(161, 'New Yugoslavia Dinar', 'YUD', '', '', NULL),
(162, 'South African Rand', 'ZAR', 'R', 'R', NULL),
(163, 'Zambian Kwacha', 'ZMK', '', '', NULL),
(164, 'Zaire Zaire', 'ZRZ', '', '', NULL),
(165, 'Zimbabwe Dollar', 'ZWD', '', '', NULL),
(166, 'Slovak Koruna', 'SKK', '', '', NULL),
(167, 'Armenian Dram', 'AMD', '', '', NULL);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Step 2:navigate your folder to application\libraries,Create new file name as Currency_lib.php after add following code this library file going to be called in controller later.

<?php  
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Currency_lib{
    private $CI;
    private $rate;
    private $fromCurrency;
    private $toCurrency;
    private $amount;
    private $hourDifference;
    function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->database(); 
        
    }
    function currencyConverter($from_Currency,$to_Currency,$amount)
    {
          $from_Currency = urlencode($from_Currency);
          $to_Currency = urlencode($to_Currency);
          $encode_amount = $amount;
          $get=file_get_contents("https://finance.google.com/finance/converter?a=$encode_amount&from=$from_Currency&to=$to_Currency");
          $get = explode("<span class=bld>",$get);
          $get = explode("</span>",$get[1]);
          $converted_currency = preg_replace("/[^0-9\.]/", null, $get[0]);
          return $converted_currency;
    }
  //Return Currency Name
  function GetCurrencyName($currency_code){
    $query=$this->CI->db->select('currency_name')->where('currency_code',$currency_code)->get('currency');
    $res=$query->row();
    if (!empty($res->currency_name))
      return (string) $res->currency_name;
    else
      return '';
  }
  //Return Currency Symbol
  function getCurrencySymbol($currency_code){
  
    $query=$this->CI->db->select('symbol')->where('currency_code',$currency_code)->get('currency');
    $res=$query->row();
    if (!empty($res->symbol))
      return (string) $res->symbol;
    else
      return '';
  }
  //Return Currency Symbol in HEX
  function getCurrencySymbolHex($currency_code){
      $query=$this->CI->db->select('hex')->where('currency_code',$currency_code)->get('currency');
    $res=$query->row();
    if (!empty($res->hex))
      return (string) $res->hex;
    else
      return '';
  }
}
?>

stpe 3:After creating libary file navigte to application/controllers,create new file name as Currency.php.After load libary currency_lib file and use following code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * summary
 */
class Currency extends CI_Controller
{
    /**
     * summary
     */
    public function __construct()
    {
        parent:: __construct();
        $this->load->library('currency_lib'); 
         $this->load->model('CurrencyModel');
    }
    public function index()
    {
      echo $this->currency_lib->GetCurrencyName('INR');
      
      $data['currency']=$this->CurrencyModel->getallCurrency();
      $this->load->view('currency-view',$data);
      
    }
    /*Convert Currency*/
    public function convert_currency()
    {
      // change amount according to your needs
      $amount =$this->input->post('txt_amt');
      // change From Currency according to your needs
      $from_Curr =$this->input->post('cur_frm');//"INR";
      // change To Currency according to your needs
      $to_Curr =$this->input->post('cur_to');
        $converted_currency=$this->currency_lib->currencyConverter($from_Curr, $to_Curr, $amount);
        // Print outout
        echo $converted_currency;
    }
}

Step 4:After creating controller file navigate folder to application/model,create new file name as CurrencyModel and use following code

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class CurrencyModel extends CI_Model
{
  public function getallCurrency()
  {
    $query=$this->db->get_where('currency',array('symbol!='=>'','hex!='=>''));
    return $query->result();
  }
}

Step 5:After creating model file navigate folder to application/views ,create new file name as currency-view and use following code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
  Currency Coverter Using Codeigniter
</title><link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" /></head>
<body>
    <form name="form1" method="post" action="<?=site_url('currency/convert_currency')?>" id="form1" >

   <div class="container" >
    <div class="row">
    <div class="col-md-6 col-md-offset-2">
    <br />
    
  <div class="panel panel-primary">
      <div class="panel-heading"><h4> Currency Coverter Using Codeigniter </h4></div>

      <div class="panel-body">
      		<div class="form-group">
          <label> Value</label>
              <input name="txt_amt" type="text" id="txt_amt" class="form-control" onkeypress="return isNumberKey(event)" placeholder="Amount you want to convert" required="" />
          </div>
         <div class="form-group">
          <label>From</label>
             <select name="cur_frm" id="cur_frm" class="form-control"  >
              <?php
              foreach ($currency as $value) 
              {
                  ?>

                  <option value="<?=$value->currency_code?>"><?=$value->currency_name?></option>
              <?php }?>

            </select>
          </div>
         <div class="form-group">
            <label>To</label>
               <select name="cur_to" id="cur_to" class="form-control">
              <?php
              foreach ($currency as $value) 
              {
                  ?>

                <option value="<?=$value->currency_code?>"><?=$value->currency_name?></option>
              <?php }?>
            

          </select>
          </div>
              </div>

        <div class="panel-footer">
             <input type="submit" name="con_Button2" value="Convert !" id="con_Button2" class="btn btn-success" />
        </div>

    </div>

    

    <SCRIPT language=Javascript>
       <!--
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode != 46 && charCode > 31
            && (charCode < 48 || charCode > 57))
                return false;

            return true;
        }
       //-->
    </SCRIPT>
</div>
</div>
</div>

    </form>
</body>
</html>

DEMO LINK: http://demo.pheonixsolutions.com/codeigniter/codeigniter-currency-converter-master/

1 thought on “Currency Conversion and Symbol library in CodeIgniter”

Leave a Reply