views:

122

answers:

2

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-->
A: 

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

RC
+2  A: 

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.

Artelius
Thanks Artelius, you are right - I am unclear how for loops work! Your code seems to do the trick. How would I add the next day? I have tried duplicating the last block and changing "$day=1" to "$day=2". This didn't work.
Jordan
Also, duplicating the last block of code without changing $day doesn't work. Any ideas?
Jordan
My guess is that the data you are provided with only contains a forecast for today and tomorrow. The for loop currently will go up to the 3rd day (due to $day < 3) but if there is no data to start with it obviously can't be shown...
Artelius