douba2705
douba2705
2013-08-22 15:06

使用字符串的案例/开关

已采纳

I have this weather script that and I'm trying to display a different image for every weather condition (case), the image will also be different if it's day or night.

Here is what I'm doing...

GETTING THE XML Info. into .PHP

    <?php
    $CapDCityANDState = "Atlanta, GA";
    $url="https://www.google.com/ig/api?weather=$CapDCityANDState&hl=en&referrer=googlecalendar";

    $xml = simplexml_load_file($url);

    //print_r($xml);

    //echo "Test: " . $xml->current_conditions->temp_f['data'];
    $DayNow = date(D);
    $DayTomorrow = date('D', strtotime('+1 day'));
    $DayAfterTomorrow = date('D', strtotime('+2 day'));
    $DayAfterAfterTomorrow = date('D', strtotime('+3 day'));


    $CurrentTxt = $xml->weather->current_conditions->condition->attributes()->data;
    $CurrentTemp = $xml->weather->current_conditions->temp_f->attributes()->data;
    //echo $CurrentTemp[0];

    echo("<BR><BR>");

    $DayOneLow = $xml->weather->forecast_conditions[0]->low->attributes()->data;
    $DayOneHi = $xml->weather->forecast_conditions[0]->high->attributes()->data;
    $DayOneTxt = $xml->weather->forecast_conditions[0]->condition->attributes()->data;
    //echo $DayOneLow[0];

    $DayTwoLow = $xml->weather->forecast_conditions[1]->low->attributes()->data;
    $DayTwoHi = $xml->weather->forecast_conditions[1]->high->attributes()->data;
    $DayTwoTxt = $xml->weather->forecast_conditions[1]->condition->attributes()->data;
    //echo $DayTwoLow[0];


    $DayTriLow = $xml->weather->forecast_conditions[2]->low->attributes()->data;
    $DayTriHi = $xml->weather->forecast_conditions[2]->high->attributes()->data;
    $DayTriTxt = $xml->weather->forecast_conditions[2]->condition->attributes()->data;
    //echo $DayTriLow[0];


    $DayFourLow = $xml->weather->forecast_conditions[3]->low->attributes()->data;
    $DayFourHi = $xml->weather->forecast_conditions[3]->high->attributes()->data;
    $DayFourTxt = $xml->weather->forecast_conditions[3]->condition->attributes()->data;
    //echo $DayFourLow[0];


    ?>

DISPLAY WEATHER FORCAST INFO.

<div>Currently: <?php echo $CurrentTemp[0]; ?>&deg; - <?php echo $CurrentTxt[0]; ?> - H: <?php echo $DayOneHi[0]; ?>&deg; L: <?php echo $DayOneLow[0]; ?>&deg; - Rest of the Day: <?php echo $DayOneTxt[0]; ?></div>
            <div><?php echo($DayTomorrow);?>: <?php echo $DayTwoHi[0]; ?>&deg;/<?php echo $DayTwoLow[0]; ?>&deg; - <?php echo $DayTwoTxt[0]; ?></div>
            <div><?php echo($DayAfterTomorrow);?>: <?php echo $DayTriHi[0]; ?>&deg;/<?php echo $DayTriLow[0]; ?>&deg; - <?php echo $DayTriTxt[0]; ?></div>
            <div><?php echo($DayAfterAfterTomorrow);?>: <?php echo $DayFourHi[0]; ?>&deg;/<?php echo $DayFourLow[0]; ?>&deg; - <?php echo $DayFourTxt[0]; ?></div>

<br>

FINDOUT IS IT DAY OR NITE CURRENTLY

<?php
$img = time() > $sunriseTime && time() < $sunsetTime ? 'day' : 'nite';
// $img <-- will say 'day' if it's day and 'nite' if its nite
?>

GET WHAT IMAGE TO SHOW DEPENDING IF IT'S DAY OR NITE AND ALSO GET THE WEATHER CONDITION TEXT AND BASE THE IMAGE ON THOSE TWO FACTORS.

<?php

if ($img == "day"){
// START OF DAY
switch ($DayOneTxt[0]) {

case "CLEAR":
    $WeatherIMG = "1.png";
    break;


case "MOSTLY CLOUDY":
    $WeatherIMG = "4.png";
    break;


case "SUNNY":
    $WeatherIMG = "2.png";
    break;


case "MOSTLY SUNNY":
    $WeatherIMG = "2.png";
    break;


case "PARTLY CLOUDY":
    $WeatherIMG = "3.png";
    break;


case "PARTLY SUNNY":
    $WeatherIMG = "3.png";
    break;


case "HAZE":
    $WeatherIMG = "5.png";
    break;


case "DUST":
    $WeatherIMG = "5.png";
    break;


case "FOG":
    $WeatherIMG = "5.png";
    break;


case "CHANCE OF STORM":
    $WeatherIMG = "6.png";
    break;


case "OVERCAST":
    $WeatherIMG = "8.png";
    break;


case "CHANCE OF RAIN":
    $WeatherIMG = "8.png";
    break;


case "CLOUDY":
    $WeatherIMG = "11.png";
    break;


case "RAIN":
    $WeatherIMG = "12.png";
    break;


case "STORM":
    $WeatherIMG = "12.png";
    break;


case "MIST":
    $WeatherIMG = "14.png";
    break;


case "LIGHT RAIN":
    $WeatherIMG = "14.png";
    break;


case "SCATTERED SHOWERS":
    $WeatherIMG = "14.png";
    break;


case "SHOWERS":
    $WeatherIMG = "14.png";
    break;


case "THUNDERSTORM":
    $WeatherIMG = "15.png";
    break;


case "SCATTERED THUNDERSTORMS":
    $WeatherIMG = "16.png";
    break;


case "CHANCE OF TSTORM":
    $WeatherIMG = "17.png";
    break;


case "FLURRIES":
    $WeatherIMG = "21.png";
    break;


case "LIGHT SNOW":
    $WeatherIMG = "21.png";
    break;


case "SCATTERED SNOW SHOWERS":
    $WeatherIMG = "21.png";
    break;


case "SNOW SHOWERS":
    $WeatherIMG = "22.png";
    break;


case "CHANCE OF SNOW":
    $WeatherIMG = "23.png";
    break;


case "SNOW":
    $WeatherIMG = "27.png";
    break;


case "RAIN AND SNOW":
    $WeatherIMG = "29.png";
    break;


case "ICE/SNOW":
    $WeatherIMG = "29.png";
    break;


case "SLEET":
    $WeatherIMG = "29.png";
    break;


case "ICY":
    $WeatherIMG = "31.png";
    break;


case "FREEZING DRIZZLE":
    $WeatherIMG = "31.png";
    break;


case "SMOKE":
    $WeatherIMG = "32.png";
    break;


case "WINDY":
    $WeatherIMG = "32.png";
    break;
}
// END OF DAY

} else {
// START OF NITE
switch ($DayOneTxt[0]) {

case "PARTLY SUNNY":
    $WeatherIMG = "33.png";
    break;

case "SCATTERED THUNDERSTORMS":
    $WeatherIMG = "42.png";
    break;

case "SHOWERS":
    $WeatherIMG = "40.png";
    break;

case "SCATTERED SHOWERS":
    $WeatherIMG = "39.png";
    break;

case "RAIN AND SNOW":
    $WeatherIMG = "29.png";
    break;

case "OVERCAST":
    $WeatherIMG = "8.png";
    break;

case "LIGHT SNOW":
    $WeatherIMG = "22.png";
    break;

case "FREEZING DRIZZLE":
    $WeatherIMG = "31.png";
    break;

case "CHANCE OF RAIN":
    $WeatherIMG = "39.png";
    break;

case "SUNNY":
    $WeatherIMG = "33.png";
    break;

case "CLEAR":
    $WeatherIMG = "33.png";
    break;

case "MOSTLY SUNNY":
    $WeatherIMG = "34.png";
    break;

case "PARTLY CLOUDY":
    $WeatherIMG = "35.png";
    break;

case "MOSTLY CLOUDY":
    $WeatherIMG = "36.png";
    break;

case "CHANCE OF STORM":
    $WeatherIMG = "42.png";
    break;

case "RAIN":
    $WeatherIMG = "40.png";
    break;

case "CHANCE OF SNOW":
    $WeatherIMG = "44.png";
    break;

case "CLOUDY":
    $WeatherIMG = "36.png";
    break;

case "MIST":
    $WeatherIMG = "39.png";
    break;

case "STORM":
    $WeatherIMG = "40.png";
    break;

case "THUNDERSTORM":
    $WeatherIMG = "41.png";
    break;

case "CHANCE OF TSTORM":
    $WeatherIMG = "42.png";
    break;

case "SLEET":
    $WeatherIMG = "29.png";
    break;

case "SNOW":
    $WeatherIMG = "43.png";
    break;

case "ICY":
    $WeatherIMG = "31.png";
    break;

case "DUST":
    $WeatherIMG = "32.png";
    break;

case "FOG":
    $WeatherIMG = "32.png";
    break;

case "SMOKE":
    $WeatherIMG = "32.png";
    break;

case "HAZE":
    $WeatherIMG = "32.png";
    break;

case "FLURRIES":
    $WeatherIMG = "25.png";
    break;

case "LIGHT RAIN":
    $WeatherIMG = "39.png";
    break;

case "SNOW SHOWERS":
    $WeatherIMG = "44.png";
    break;

case "ICE/SNOW":
    $WeatherIMG = "43.png";
    break;

case "WINDY":
    $WeatherIMG = "32.png";
    break;

case "SCATTERED SNOW SHOWERS":
    $WeatherIMG = "43.png";
    break;

}
// END OF NITE
}
    ?>

SHOW WHAT IMAGE NAME TO USE e.g. "32.png"

<?php echo($WeatherIMG); ?>

The image always shows "1.png" and that is wrong :(

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhi4056 douzhi4056 8年前
    <?php
    $txtDay = Array( "clear"=>1, "sunny"=>2, "partly cloudy"=>3, "mostly cloudy"=>4, "haze"=>5, "fog"=>5, "dust"=>5, ............. "scatter snow showers"=>43);
    $txtNight = Array( "clear"=>1, "sunny"=>2, "partly cloudy"=>3, "mostly cloudy"=>4, "haze"=>5, "fog"=>5, "dust"=>5, ............. "scatter snow showers"=>43);
    if ($img == "day"){
        $imgWeather = $txtDay[{$DayOneTxt[0]}] . ".png";
    } else {
        $imgWeather = $txtNight[{$DayOneTxt[0]}] . ".png";
    }
    ?>
    
    点赞 评论 复制链接分享