在php中调用变量

时间:2013-05-18 10:42:51

标签: php xml parsing loops

我在php中编写了两个单独的脚本。其中一个是解析xml数据,另一个是日期排序。

xml解析

<?php 


$date = date ("d.m.Y");
$url = "http://www.cbar.az/currencies/" .$date. ".xml";


$doc = new DOMDocument(); 

$doc->load($url); 

$valyutalar = $doc->getElementsByTagName( "Valute" ); 


foreach( $valyutalar as $valyuta ) 
{ 
  $nominal = $valyuta->getElementsByTagName( "Nominal" ); 
  $nom = $nominal->item(0)->nodeValue; 

  $code = trim($valyuta->getAttribute('Code'));


  $value = $valyuta->getElementsByTagName( "Value" ); 
  $val = $value->item(0)->nodeValue;   

 echo "<b>".$code." - " .$val. "AZN\n</b><br>-----------------------<br>";


  }
   ?>

日期

<?php
  date_default_timezone_set('UTC');
  $beginning = "25.11.1993";
  $ending = date("d.m.Y");
  $date = $beginning;


  while(strtotime($date) <= strtotime($ending))
  {
  /* echo "$date<br />"; */
  $date = date ("d.m.Y", strtotime("+1 day", strtotime($date)));
  }
?>

我想加入这两个,用于解析从1993年到现在的数据。我知道我必须把解析器放在while循环中,但不知道究竟放在哪里。任何人都可以给出一个阿基米德点只是为了开始吗? - ))

1 个答案:

答案 0 :(得分:1)

如果$ date在你的while循环中正确迭代+1天,那么:

while(strtotime($date) <= strtotime($ending))
  {
  /* echo "$date<br />"; */
  $date = date ("d.m.Y", strtotime("+1 day", strtotime($date)));

 $url = "http://www.cbar.az/currencies/" .$date. ".xml";


$doc = new DOMDocument(); 

$doc->load($url); 

$valyutalar = $doc->getElementsByTagName( "Valute" ); 


foreach( $valyutalar as $valyuta ) 
{ 
  $nominal = $valyuta->getElementsByTagName( "Nominal" ); 
  $nom = $nominal->item(0)->nodeValue; 

  $code = trim($valyuta->getAttribute('Code'));


  $value = $valyuta->getElementsByTagName( "Value" ); 
  $val = $value->item(0)->nodeValue;   

 echo "<b>".$code." - " .$val. "AZN\n</b><br>-----------------------<br>";


  }
}$u
   ?>

所以如果$ date递增,每次触发while循环时,它会在$ date的下一次迭代中加载$ url