how to code a weather forecast? here is what I have: $data['list'] [0] ['weather'] [0] ['description'], 'icon' => $data['list'] [0] ['weather'] [0] ['icon'], 'maxtemp' => $data['list'] [0] ['temp'] ['max'], 'mintemp' => $data['list'] [0] ['temp'] ['min'], 'humidity' => $data['list'] [0] ['humidity'], 'speed' => $data['list'] [0] ['speed'] ); return $weather; } // Check to see if the web FORM was submitted with a city. if (FALSE) { // Call your function passing it the city and assign the result to the variable $forecast. if ($forecast) { echo ' /* @var $city type */ ' . $city . ' Weather Forecast ' . date("l g:i a", $currentTime) . ' ' . date("jS F, Y", $currentTime) . ' ' . ucwords($forecast['description']) . ' High ' . $forecast['maxtemp'] . '°C Low ' . $forecast['mintemp'] . '°C Humidity: ' . $forecast['humidity'] . ' % Wind: ' . $forecast['speed'] . ' km/h '; } } else { echo ' City: '; } ?>
how to code a weather forecast?
here is what I have:
<?php
//code
$currentTime = time();
// Create a function called getForecast that accepts a city as a parameter and returns an array with the
// the weather description, icon, maxtemp, mintemp, humidity and wind speed.
function foo () {
$country = "US";
$url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=" . $city . "," . $country . "&units=metric&cnt=1&lang=en&appid=c0c4a4b4047b97ebc5948ac9c48c0559";
$json = file_get_contents($url);
// json_decode converts a JSON object into a PHP array. You will need to use var_dump or print_r to view the structure of the data returned to build your $weather array
$data = json_decode($json, true);
$weather = array(
'description' => $data['list'] [0] ['weather'] [0] ['description'],
'icon' => $data['list'] [0] ['weather'] [0] ['icon'],
'maxtemp' => $data['list'] [0] ['temp'] ['max'],
'mintemp' => $data['list'] [0] ['temp'] ['min'],
'humidity' => $data['list'] [0] ['humidity'],
'speed' => $data['list'] [0] ['speed']
);
return $weather;
}
// Check to see if the web FORM was submitted with a city.
if (FALSE) {
// Call your function passing it the city and assign the result to the variable $forecast.
if ($forecast) {
echo '
/* @var $city type */
<h2>' . $city . ' Weather Forecast</h2>
<div class="time">
<div>' . date("l g:i a", $currentTime) . '</div>
<div>' . date("jS F, Y", $currentTime) . '</div>
<div>' . ucwords($forecast['description']) . '</div>
</div>
<div class="weather-forecast">
<img src="https://openweathermap.org/img/w/' . $forecast['icon'] . '.png" class="weather-icon" />
High ' . $forecast['maxtemp'] . '°C <span class="min-temperature">
Low ' . $forecast['mintemp'] . '°C</span>
</div>
<div class="time">
<div>Humidity: ' . $forecast['humidity'] . ' %</div>
<div>Wind: ' . $forecast['speed'] . ' km/h</div>
</div>';
}
} else {
echo '
<form method="POST" action="weatherforecast.php">
City: <input type="text" name="city"/><br/><br/>
<input type="submit" name="forecast" value="Get Forecast"/>
</form>
';
}
?>


Trending now
This is a popular solution!
Step by step
Solved in 3 steps









