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-->
Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
Jordan
  • 119
  • 3
  • 11
  • Well, your code seems a bit messy. for start, why do you use "for" conditions like that? Also, you seem to be closing paragraph statements without starting them at all. About the code, does the weather xml provide the timestamp / numeric date for the days, or just string values? – yoda Oct 26 '09 at 06:04
  • @yoda: The paragraphs are OK. `` closes a paragraph whereas `

    ` opens *and* closes the paragraph. It's the `
    `s that worry me.
    – Artelius Oct 26 '09 at 06:10
  • Sorry guys, I am a bit of a n00b at this business :) Please forgive me! – Jordan Oct 26 '09 at 06:13

2 Answers2

2

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
  • 48,337
  • 13
  • 89
  • 105
  • 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 Oct 26 '09 at 06:20
  • Also, duplicating the last block of code without changing $day doesn't work. Any ideas? – Jordan Oct 26 '09 at 06:31
  • 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 Oct 27 '09 at 01:53
0

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