一款实用的PHP货币换算程序代码哦,有需要的朋友可以参考一下。
<?php /* * 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 is 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 is distributed in the hope that it will 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 for 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 was 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, 80, $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 = "{<Cubes*currency='(w*)'s*rate='([d.]*)'/>}is"; preg_match_all($pattern, $buffer, $xml_rates); array_shift($xml_rates); for ($i = 0; $i < count($xml_rates[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 you need to make a conversion just include the class file and call the ‘convert’ function. You will need to enter your own mysql database variables such as the login details. The example below will convert £2.50 GBP into US Dollars ($).
<?php 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'); ?>
教程链接:
随意转载~但请保留教程地址★

使用golang进行SelectChannelsGo并发式编程的异步处理方法引言:并发式编程是现代软件开发中的一个重要领域,它可以有效地提高应用程序的性能和响应能力。在Go语言中,使用Channels和Select语句可以简单而高效地实现并发编程。本文将介绍如何使用golang进行SelectChannelsGo并发式编程的异步处理方法,并提供具体的

jquery隐藏select元素的方法:1、hide()方法,在HTML页面中引入jQuery库,可以使用不同选择器来隐藏select元素,ID选择器将selectId替换为你实际使用的select元素的ID;2、css()方法,使用ID选择器选择需要隐藏的select元素,使用css()方法将display属性设置为none,并将selectId替换为select元素的ID。

jQuery是一个流行的JavaScript库,可以用来简化DOM操作、事件处理、动画效果等。在web开发中,经常会遇到需要对select元素进行改变事件绑定的情况。本文将介绍如何使用jQuery实现对select元素改变事件的绑定,并提供具体的代码示例。首先,我们需要使用标签来创建一个包含选项的下拉菜单:

因为select可以使开发者在同时等待多个文件缓冲区,可减少IO等待的时间,能够提高进程的IO效率。select()函数是IO多路复用的函数,允许程序监视多个文件描述符,等待所监视的一个或者多个文件描述符变为“准备好”的状态;所谓的”准备好“状态是指:文件描述符不再是阻塞状态,可以用于某类IO操作了,包括可读,可写,发生异常三种。select是一个计算机函数,位于头文件#include。该函数用于监视文件描述符的变化情况——读写或是异常。1.select函数介绍select函数是IO多路复用的函

1、SQL语句中的关键词对大小写不敏感,SELECT等效于SELECT,FROM等效于from。2、从users表中选择所有列的,可以用符号*代替列的名称。语法--这是注释--从FEOM指定的[表中],查询出[所有的]数据.*表示[所有列]SELECT*FROM--通过从FROM从指定的[表中],查询出指定列名称(字段)的数据SELECT列名称FROM表名称实例--注意:多个列之间,使用英文的逗号来分隔selectusername,passwordfrom

通过golang实现SelectChannelsGo并发式编程的性能优化在Go语言中,使用goroutine和channel实现并发编程是非常常见的。而在处理多个channel的情况下,我们通常会使用select语句来进行多路复用。但是,在大规模并发的情况下,使用select语句可能会导致性能下降。在本文中,我们将介绍一些通过golang实现select

使用Golang实现可靠性和鲁棒性的SelectChannelsGo并发式编程引言:在现代软件开发中,并发性已经成为了一个非常重要的主题。使用并发编程可以使得程序更具有响应性、更高效地利用计算资源,并且能够更好地处理大规模的并行计算任务。Golang是一种非常强大的并发编程语言,它通过go协程和channel机制,提供了一种简单而有效的方式来实现并发编程

PHP的preg_match()函数:如何使用正则表达式匹配字符串,需要具体代码示例正则表达式在字符串处理中是非常强大和灵活的工具。在PHP中,使用preg_match()函数可以方便地进行字符串的正则匹配,从而实现各种复杂的模式匹配和替换操作。本文将介绍preg_match()函数的用法,并提供具体的代码示例来帮助读者更好地理解和应用。preg_match


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

뜨거운 주제



