热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP货币换算程序代码

一款实用的PHP货币换算程序代码哦,有需要的朋友可以参考一下。?phpFile:CurrencyConverter.php...
一款实用的PHP货币换算程序代码哦,有需要的朋友可以参考一下。

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 = "{}is";
            preg_match_all($pattern, $buffer, $xml_rates);
            array_shift($xml_rates);
            for ($i = 0; $i 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 ($).

convert(2.50, 'GBP', 'USD');
?>


教程链接:

随意转载~但请保留教程地址★

推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 分享css中提升优先级属性!important的用法总结
    web前端|css教程css!importantweb前端-css教程本文分享css中提升优先级属性!important的用法总结微信门店展示源码,vscode如何管理站点,ubu ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • Redis底层数据结构之压缩列表的介绍及实现原理
    本文介绍了Redis底层数据结构之压缩列表的概念、实现原理以及使用场景。压缩列表是Redis为了节约内存而开发的一种顺序数据结构,由特殊编码的连续内存块组成。文章详细解释了压缩列表的构成和各个属性的含义,以及如何通过指针来计算表尾节点的地址。压缩列表适用于列表键和哈希键中只包含少量小整数值和短字符串的情况。通过使用压缩列表,可以有效减少内存占用,提升Redis的性能。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
author-avatar
woorain_77b002
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有