开发者

Why isn't this PHP parsing XML correctly?

开发者 https://www.devze.com 2022-12-09 16:07 出处:网络
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.

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.weat开发者_StackOverflow社区her.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-->


Either you're not very clear on how for loops work or you've just made a really silly error.

In case of the former, remember that

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

is equivalent to

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

It looks like you are only getting forecasts for today and tomorrow; your first loop produces:

todayMon______________19

todayTue______________26

Your second loop produces:

Tue______________26

And your third loop produces nothing.

You should probably change your code to something like this:

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

Another comment: I see you using <p /> however you also use <br>, this is puzzling. <br> is not valid XHTML.


I assume $weather_adelaide->forecast[0] and $weather_adelaide->forecast[1] are set so you get your first for to print 2 times and your second to print one. I think you need if and not for: (not tested)

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

But I would go with a foreach(range(0, 2) as $i) and handle the special today case with a if

0

精彩评论

暂无评论...
验证码 换一张
取 消