tags:

views:

65

answers:

1

Hello,

I have to create an automatic weather including rain, snow, clouds, fog and sunny.

Depending on the season I need to set a percentage for all weather: the forecast will be updated 3 or 4 times during a day.

Example: Winter | Rain: 30% Snow: 30% Sunny: 10% Cloudy: 10%, Fog: 20%

I do not know how to implement a random condition based on percentages. Some help?

Many thanks and sorry for my bad English.

A: 

Well, you can use:

$location = 'Rome';
$document = file_get_contents(str_replace(" ", "+", "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=".$location));
$xml = new SimpleXMLElement($document); 
echo "$location: ".$xml->temp_c."° C"; 

Just take a look on the XML and see what data you have available.

EDIT

I didn't understand what the OP wanted the first time. Basically, it's even easier.

$weather = mt_rand(0,100);
$season = 'winter';
switch($season) {
    case 'winter': {
        if ($weather < 30) {
            $output = 'Rainy';
        } else if ($weather >=30 && $weather < 60) {
            $output = 'Snowy';
        }
        // goes on on the same ideea of testing the value of $weather
        break;
    }
    // other seasons 
} 

echo $output;

What I suggest tough, is to keep your values in arrays (for example the seasons) as well as the values for chances to have one type of weather or another.

array (
   [winter] => array (
       [30] => 'Rainy',
       [60] => 'Snowy',
       ... // the other chances of weather
   ),
   [spring] => array (
       ...
   ), 
   ... // and so on
)

Use mt_rand(0,100) to get a random value and the array above to determine the weather.

Please let me know if this works for you.

Claudiu
Thank you for reply.The problem is that I have to implement a random condition based on percentages, not using api.I can not understand, once I determined the season, how to use percentages to determine the condition until the next weather update.Like this:in Winter | Rain: 30% Snow: 30% Sunny: 10% Cloudy: 10%, Fog: 20%in Autumn | Rain: 20% Snow: 0% Sunny: 30% Cloudy: 40%, Fog: 10%With these parameters, the script should produce a weather condition.
Diego
Oh, so basically, you have 30% chances of rain, 30% chances of snow, 10% chances of sun, 10% chances of clouds and 20% chances of fog in the winter for example?
Claudiu
@Claudio Exactly.
Diego
@Diego I edited my response above according to the requirements
Claudiu
@ClaudiuOk, Thanks. I just have created a script like your example.
Diego
Great, does it work as expected?
Claudiu