0

I am attempting to parse Yahoo's weather XML feed via this script. The parsing itself works: I am just struggling with getting the days to correspond with today, tomorrow and the day after.

The final HTML output looks like this:

Which can be seen here: http://www.wdmadvertising.com.au/preview/cfs/index.shtml

todayMon______________19

todayTue______________26

Tue______________26

It is supposed to look like this:

Today______________(temp)

(tomrrow)______________(temp)

(day after tomorrow)______________(temp)

The PHP and 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-->
4

2 回答 2

2

要么你不是很清楚for循环是如何工作的,要么你只是犯了一个非常愚蠢的错误。

如果是前者,请记住

for($x=0; isset(blah); $x++) {
    ...
}

相当于

$x = 0;
while(isset(blah)) {
    ...
    $x++;
}

看起来您只能获得今天和明天的预测;你的第一个循环产生:

todayMon______________19

todayTue______________26

您的第二个循环产生:

Tue______________26

而你的第三个循环什么也没产生。

您可能应该将代码更改为以下内容:

// 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>";
}

另一条评论:我看到你在使用,<p />但你也在使用<br>,这令人费解。<br>不是有效的 XHTML。

于 2009-10-26T06:08:01.347 回答
0

我假设$weather_adelaide->forecast[0]$weather_adelaide->forecast[1]设置好让你第一次for打印 2 次,第二次打印一次。我认为你需要if而不是for:(未测试)

// TODAY 1
$day = 0;
if(isset($weather_adelaide->forecast[$day])) {
    print "<h2>today".$weather_adelaide->forecast[$day]['DAY']."</h2>";     
    print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>"; 
}

// FORECAST 2
++$day;
if (isset($weather_adelaide->forecast[$day])) {
    print "<h2>".$weather_adelaide->forecast[$day]['DAY']."</h2>";     
    print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>"; 
}

// FORECAST 3       
++$day;
if (isset($weather_adelaide->forecast[$day]))  {
    print "<h2>".$weather_adelaide->forecast[$day]['DAY']."</h2>";              
    print "<p />".$weather_adelaide->forecast[$day]['HIGH']."<br>"; 
}

但我会选择 aforeach(range(0, 2) as $i)并用 a 处理今天的特殊情况if

于 2009-10-26T06:03:21.767 回答