为什么不是';这个PHP不能正确解析XML吗?
我试图通过脚本解析雅虎的天气XML提要。解析本身是有效的:我只是在努力让这些天与今天、明天和后天保持一致 最终的HTML输出如下所示:为什么不是';这个PHP不能正确解析XML吗?,php,html,xml,parsing,yahoo-weather-api,Php,Html,Xml,Parsing,Yahoo Weather Api,我试图通过脚本解析雅虎的天气XML提要。解析本身是有效的:我只是在努力让这些天与今天、明天和后天保持一致 最终的HTML输出如下所示: Today______________(temp) (tomrrow)______________(temp) (day after tomorrow)______________(temp) // TODAY 1 if (isset($weather_adelaide->forecast[0])) { print "<h2>t
Today______________(temp)
(tomrrow)______________(temp)
(day after tomorrow)______________(temp)
// TODAY 1
if (isset($weather_adelaide->forecast[0])) {
print "<h2>today</h2>";
print "<p />".$weather_adelaide->forecast[0]['HIGH']."<br>";
}
// More days
for ($day=1; $day < 3 && isset($weather_adelaide->forecast[$day]); $day++) {
print "<h2>".$weather_adelaide->forecast[$day]['DAY']."</h2>";
print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>";
}
可以在这里看到:
应该是这样的:
Today______________(temp)
(tomrrow)______________(temp)
(day after tomorrow)______________(temp)
// TODAY 1
if (isset($weather_adelaide->forecast[0])) {
print "<h2>today</h2>";
print "<p />".$weather_adelaide->forecast[0]['HIGH']."<br>";
}
// More days
for ($day=1; $day < 3 && isset($weather_adelaide->forecast[$day]); $day++) {
print "<h2>".$weather_adelaide->forecast[$day]['DAY']."</h2>";
print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>";
}
PHP和HTML:
<div class="latest-weather">
<h1 class="latest-weather">Latest weather</h1>
include("class.xml.parser.php");
include("class.weather.php");
$weather_adelaide = new weather("ASXX0001", 3600, "c", $cachedir);
$weather_adelaide->parsecached();
// TODAY 1
for ($day=0; isset($weather_adelaide->forecast[$day]); $day++) {
print "<h2>today".$weather_adelaide->forecast[$day]['DAY']."</h2>";
print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>"; }
// FORECAST 2
for ($day=1; isset($weather_adelaide->forecast[$day]); $day++) {
print "<h2>".$weather_adelaide->forecast[$day]['DAY']."</h2>";
print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>"; }
// FORECAST 3
for ($day=2; isset($weather_adelaide->forecast[$day]); $day++) {
print "<h2>".$weather_adelaide->forecast[$day]['DAY']."</h2>";
print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>"; }
?>
</div><!--/latest-weather-->
最新天气
包括(“class.xml.parser.php”);
包括(“class.weather.php”);
$weather_adelaide=新天气(“ASXX0001”,3600,“c”,$cachedir);
$weather_adelaide->parsecached();
//今天1
对于($day=0;isset($weather_adelaide->forecast[$day]);$day++){
打印“今天”。$weather_adelaide->forecast[$day]['day'”;
打印“”$weather_adelaide->forecast[$day]['HIGH']。”
“;}
//预测2
对于($day=1;isset($weather_adelaide->forecast[$day]);$day++){
打印“$weather_adelaide->forecast[$day]['day'”;
打印“”$weather_adelaide->forecast[$day]['HIGH']。”
“;}
//预测3
对于($day=2;isset($weather_adelaide->forecast[$day]);$day++){
打印“$weather_adelaide->forecast[$day]['day'”;
打印“”$weather_adelaide->forecast[$day]['HIGH']。”
“;}
?>
我假设设置了$weather\u adelaide->forecast[0]
和$weather\u adelaide->forecast[1]
,这样您的第一个将打印两次,第二个将打印一次。我想你需要如果而不是对于:(未测试)
//今天1
$day=0;
如果(isset($weather\u adelaide->forecast[$day])){
打印“今天”。$weather_adelaide->forecast[$day]['day'”;
打印“”$weather_adelaide->forecast[$day]['HIGH']。“
”;
}
//预测2
++$day;
如果(isset($weather\u adelaide->forecast[$day])){
打印“$weather_adelaide->forecast[$day]['day'”;
打印“”$weather_adelaide->forecast[$day]['HIGH']。“
”;
}
//预测3
++$day;
如果(isset($weather\u adelaide->forecast[$day])){
打印“$weather_adelaide->forecast[$day]['day'”;
打印“”$weather_adelaide->forecast[$day]['HIGH']。“
”;
}
但是我会使用foreach(范围(0,2)为$I)
并使用if
处理今天的特殊情况,或者您不太清楚for
循环是如何工作的,或者您刚刚犯了一个非常愚蠢的错误
对于前者,记住这一点
for($x=0; isset(blah); $x++) {
...
}
相当于
$x = 0;
while(isset(blah)) {
...
$x++;
}
看起来你只得到今天和明天的预测;第一个循环生成:
todayMon______________19
todayTue______________26
Tue______________26
第二个循环生成:
todayMon______________19
todayTue______________26
Tue______________26
第三个循环什么也不产生
您可能应该将代码更改为以下内容:
Today______________(temp)
(tomrrow)______________(temp)
(day after tomorrow)______________(temp)
// TODAY 1
if (isset($weather_adelaide->forecast[0])) {
print "<h2>today</h2>";
print "<p />".$weather_adelaide->forecast[0]['HIGH']."<br>";
}
// More days
for ($day=1; $day < 3 && isset($weather_adelaide->forecast[$day]); $day++) {
print "<h2>".$weather_adelaide->forecast[$day]['DAY']."</h2>";
print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>";
}
//今天1
如果(isset($weather_adelaide->forecast[0])){
打印“今日”;
打印“”$weather_adelaide->forecast[0]['HIGH']。“
”;
}
//更多天
对于($day=1;$day<3&&isset($weather_adelaide->forecast[$day]);$day++){
打印“$weather_adelaide->forecast[$day]['day'”;
打印“”$weather_adelaide->forecast[$day]['HIGH']。“
”;
}
另一条评论:我看到您使用了
,但您也使用了
,这令人费解<代码>
不是有效的XHTML。嗯,您的代码似乎有点凌乱。首先,为什么要使用“for”这样的条件?此外,您似乎在结束段落语句时根本没有开始它们。关于代码,weather xml是提供日期的时间戳/数字日期,还是只提供字符串值?@yoda:这些段落都可以
关闭段落,而
打开并关闭段落。是
让我担心。对不起,伙计们,我在这件事上有点神经过敏:)请原谅我!谢谢Artelius,你是对的-我不清楚循环是如何工作的!你的代码似乎做到了这一点。第二天我将如何添加?我已尝试复制最后一个块,并将“$day=1”更改为“$day=2”。这不起作用。而且,在不更改$day的情况下复制最后一段代码也不起作用。有什么想法吗?我猜你得到的数据只包含今天和明天的预测。for循环当前将上升到第三天(由于$day<3),但是如果没有数据开始,它显然无法显示。。。