0

このスクリプトを使用して、 Yahoo の天気 XML フィードを解析しようとしています。解析自体は機能します。今日、明日、明後日に対応する日を取得するのに苦労しています。

最終的な HTML 出力は次のようになります。

ここで見ることができます: http://www.wdmadvertising.com.au/preview/cfs/index.shtml

todayMon______________19

todayTue______________26

Tue______________26

次のようになります。

Today______________(temp)

(tomrrow)______________(temp)

(day after tomorrow)______________(temp)

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-->
4

2 に答える 2

2

ループがどのように機能するかについてあまり明確ではないかfor、本当にばかげたエラーを犯しただけです。

前者の場合、覚えておいてください。

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

と同等です

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

今日と明日の予報しか出ていないようです。あなたの最初のループは以下を生成します:

todayMon______________19

todayTue______________26

2 番目のループは次を生成します。

Tue______________26

そして、3 番目のループでは何も生成されません。

おそらく、コードを次のように変更する必要があります。

// 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

最初に2回印刷し、2番目に1回印刷するように設定されている$weather_adelaide->forecast[0]と思います。私はあなたが必要としないと思います:(テストされていません)$weather_adelaide->forecast[1]foriffor

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

しかし、私は で行きforeach(range(0, 2) as $i)、特別な今日のケースを で処理しますif

于 2009-10-26T06:03:21.767 に答える