PHP Currency Conversion program Code _php Tutorial

Source: Internet
Author: User
A Practical PHP currency conversion program code Oh, the need for friends can refer to.
The code is as follows Copy Code

/*
* File:CurrencyConverter.php
* Author:simon Jarvis
* copyright:2005 Simon Jarvis
* date:10/12/05
* link:http://www.white-hat-web-design.co.uk/articles/php-currency-conversion.php

*
* This program was free software; You can redistribute it and/or
* Modify it under the terms of the GNU general public License
* As published by the Free software Foundation; Either version 2
* of the License, or (at your option) any later version.
*
* This program was distributed in the hope that it'll be useful,
* but without any WARRANTY; Without even the implied warranty of
* merchantability or FITNESS for A particular PURPOSE. See the
* GNU general public License-more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

Class Currencyconverter {

var $xml _file = "Www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
var $mysql _host, $mysql _user, $mysql _pass, $mysql _db, $mysql _table;
var $exchange _rates = Array ();

Load Currency Rates

function Currencyconverter ($host, $user, $pass, $db, $TB) {

$this->mysql_host = $host;
$this->mysql_user = $user;
$this->mysql_pass = $pass;
$this->mysql_db = $db;
$this->mysql_table = $TB;

$this->checklastupdated ();

$conn = mysql_connect ($this->mysql_host, $this->mysql_user, $this->mysql_pass);

$rs = mysql_select_db ($this->mysql_db, $conn);

$sql = "SELECT * from". $this->mysql_table;

$rs = mysql_query ($sql, $conn);

while ($row = Mysql_fetch_array ($rs)) {

$this->exchange_rates[$row [' currency ']] = $row [' Rate '];
}

}

/* Perform The actual conversion, defaults to£1.00 GBP to USD */
function convert ($amount =1, $from = "GBP", $to = "USD", $decimals =2) {

Return (Number_format ($amount/$this->exchange_rates[$from]) * $this->exchange_rates[$to], $decimals));
}

/* Check to see how long since the data is last updated */
function checklastupdated () {
$conn = mysql_connect ($this->mysql_host, $this->mysql_user, $this->mysql_pass);

$rs = mysql_select_db ($this->mysql_db, $conn);

$sql = "SHOW TABLE STATUS from". $this->mysql_db. " Like ' ". $this->mysql_table." ";

$rs = mysql_query ($sql, $conn);

if (mysql_num_rows ($rs) = = 0) {

$this->createtable ();
} else {
$row = Mysql_fetch_array ($RS);
if (Time () > (Strtotime ($row ["Update_time"]) + (12*60*60)) {

$this->downloadexchangerates ();
}
}
}

/* Download XML file, extract exchange rates and store values in database */

function Downloadexchangerates () {
$currency _domain = substr ($this->xml_file,0,strpos ($this->xml_file, "/"));
$currency _file = substr ($this->xml_file,strpos ($this->xml_file, "/"));
$fp = @fsockopen ($currency _domain, $errno, $ERRSTR, 10);
if ($fp) {

$out = "GET". $currency _file. " Http/1.1rn ";
$out. = "Host:". $currency _domain. " RN ";
$out. = "user-agent:mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.8) gecko/20051111 Firefox/1.5rn ";
$out. = "Connection:closernrn";
Fwrite ($fp, $out);
while (!feof ($fp)) {

$buffer. = Fgets ($fp, 128);
}
Fclose ($FP);

$pattern = "{ }is ";
Preg_match_all ($pattern, $buffer, $xml _rates);
Array_shift ($xml _rates);

for ($i =0; $i

$exchange _rate[$xml _rates[0][$i]] = $xml _rates[1][$i];
}

$conn = mysql_connect ($this->mysql_host, $this->mysql_user, $this->mysql_pass);

$rs = mysql_select_db ($this->mysql_db, $conn);

foreach ($exchange _rate as $currency = + $rate) {

if ((Is_numeric ($rate)) && ($rate! = 0)) {

$sql = "SELECT * from". $this->mysql_table. " WHERE currency= ' ". $currency." ' ";
$rs = mysql_query ($sql, $conn) or Die (Mysql_error ());
if (mysql_num_rows ($rs) > 0) {

$sql = "UPDATE". $this->mysql_table. " SET rate= ". $rate." WHERE currency= ' ". $currency." ' ";
} else {

$sql = "INSERT into". $this->mysql_table. " VALUES (' ". $currency." ', ". $rate.") ";
}

$rs = mysql_query ($sql, $conn) or Die (Mysql_error ());
}

}
}
}

/* Create The Currency exchange table */
function CreateTable () {

$conn = mysql_connect ($this->mysql_host, $this->mysql_user, $this->mysql_pass);

$rs = mysql_select_db ($this->mysql_db, $conn);

$sql = "CREATE TABLE". $this->mysql_table. " (Currency char (3) NOT NULL default ", rate float not null default ' 0 ', PRIMARY KEY (currency)) Engine=myisam ";

$rs = mysql_query ($sql, $conn) or Die (Mysql_error ());

$sql = "INSERT into". $this->mysql_table. " VALUES (' EUR ', 1) ";

$rs = mysql_query ($sql, $conn) or Die (Mysql_error ());

$this->downloadexchangerates ();
}

}

?>


Copy The above code into a new file and save it as currencyconverter.php. Whenever need to make a conversion just include the class file and the ' Convert ' function. You'll need to enter your own MySQL database variables such as the login details. The example below would convert£2.50 GBP into US Dollars ($).

The code is as follows Copy Code

Include (' currencyconverter.php ');
$x = new Currencyconverter (' Your_host ', ' your_username ', ' your_password ', ' your_database_name ', ' your_table_name ');
echo $x->convert (2.50, ' GBP ', ' USD ');
?>

http://www.bkjia.com/PHPjc/631698.html www.bkjia.com true http://www.bkjia.com/PHPjc/631698.html techarticle A practical PHP currency conversion program code Oh, the need for friends can refer to. Code to copy code like this? PHP/* * File:CurrencyConverter.php * author:simon Jarvis * Copy ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.