【问题标题】:php currency slows pagephp 货币减慢页面
【发布时间】:2014-10-24 08:14:59
【问题描述】:

我有一个带有广告的页面,我将页面货币设置为“RON”,然后我转换为也以“Euro”显示,但在循环中非常慢。我尝试包含其他 php 的脚本,但仍然如此同样...我尝试了许多货币兑换器,但都有相同的问题..减慢页面速度..如果我将代码直接放入循环中,则会告诉我一个错误:无法重复该类。 这是我使用的 php 货币:

<?php 
class cursBnrXML
 {

     var $currency = array();

    function cursBnrXML($url)
    {
        $this->xmlDocument = file_get_contents($url);
        $this->parseXMLDocument();
    }


    function parseXMLDocument()
    {
         $xml = new SimpleXMLElement($this->xmlDocument);

         $this->date=$xml->Header->PublishingDate;

         foreach($xml->Body->Cube->Rate as $line)    
         {                      
             $this->currency[]=array("name"=>$line["currency"], "value"=>$line, "multiplier"=>$line["multiplier"]);
         }
    }
    function getCurs($currency)
    {
        foreach($this->currency as $line)
        {
            if($line["name"]==$currency)
            {
                return $line["value"];
            }
        }

        return "Incorrect currency!";
    }
 }
//@an example of using the cursBnrXML class
$curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
 ?>

【问题讨论】:

    标签: php currency


    【解决方案1】:

    您可以将cursBnrXML 类修改为缓存已解析的货币,这样您就不必在每次查找时再次循环整个集合。

    <?php 
    class cursBnrXML
     {
    
        private $_currency = array();
    
        # keep the constructor the same
    
    
        # modify this method
        function parseXMLDocument()
        {
             $xml = new SimpleXMLElement($this->xmlDocument);
    
             $this->date=$xml->Header->PublishingDate;
    
             foreach($xml->Body->Cube->Rate as $line)    
             {                      
                 $this->currency[$line["currency"]]=array(
                    "value"=>$line, 
                    "multiplier"=>$line["multiplier"]
                 );
             }
        }
    
        # modify this method too
        function getCurs($currency)
        {
            if (isset($this->_currency[$currency])) 
            {
                 return $this->_currency[$currency]['value'];
            }
    
            return "Incorrect currency!";
        }
     }
    //@an example of using the cursBnrXML class
    $curs=new cursBnrXML("http://www.bnr.ro/nbrfxrates.xml");
     ?>
    

    【讨论】:

    • 感谢您的回复.. 但是 php 中处于初学者状态的新功能.. 也许您可以帮助我编写一些代码...
    • 以上代码是你cursBnrXML类的修改版。
    • 某处到底是一个错误(解析错误:语法错误,意外'{')在脚本的末尾某处:`function getCurs($currency) { if (isset($this ->_currency[$currency]) { return $this->_currency[$currency]['v​​alue']; } return "货币不正确!"; }`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多