Is it possible to use an if isset on an item in an array?
example:
if (isset($cityWeatherRssUrls[0])
I receive the following warning when doing so.
Warning: get_temperature() [function.get-temperature]: Node no longer exists in C:\xampp\htdocs\Twinz\includes\getWeather.php on line 51
Line 51 begins after the //Pull temperature comment from the function get_temperature(SimpleXMLElement $xml) in my getWeather.php script.
Below contains all the relevant scripts and functions
cityConfig.php – is used for declaring all the cities and corresponding URL’s to yahoo’s weather XML.
// Define arrays //
$cities = array();
$cityWeatherRssUrls = array();
// Feed URL's //
$yahooWeather = 'http://weather.yahooapis.com/forecastrss?p=';
// City 1 //
$city = 'London';
$cityWeatherRssUrl = $yahooWeather . $cityWeatherFeedCode . '&u=c';
array_push($cities, $city);
array_push($cityWeatherRssUrls, $cityWeatherRssUrl);
getWeather.php – contains functions for parsing XML tag contents and displaying the content
function get_current_weather($weatherRssUrl) {
// Get XML data from source
if (isset($weatherRssUrl)) {
$feed = $weatherRssUrl;
} else {
echo 'Feed not found. Check URL';
}
checkFeedExists($feed);
$xml = new SimpleXmlElement($feed);
$weather = get_temperature($xml);
return $weather;
}
function get_temperature(SimpleXMLElement $xml) {
// Pull temperature from XML
$weather['temp'] = $xml->channel->item->children('yweather', TRUE)->condition->attributes()->temp;
echo round($weather['temp']) . "°C" . "<br />";
return $temp;
}
//Display City Content //
function displayCityContent($cityWeatherRssUrls, $columnSubheading) {
if (isset($cityWeatherRssUrls)) {
echo get_current_weather($cityWeatherRssUrls);
echo $columnSubheading;
echo get_forecast_weather($cityWeatherRssUrls);
echo '<br />';
} else {
content_unavailable();
}
}
threeColumnContainer.php – is used to display Weather contents inside a html column
<ul class="columns">
<li class="col1">
<h3><?php
if (isset($column1Heading)) {
echo $column1Heading;
}
?></h3>
<p>
<?php
if (isset($cityWeatherRssUrls[0]) && ($currentPage == 1)) {
echo displayCityContent($cityWeatherRssUrls[0], $columnSubheading);
I can confirm that $cityWeatherRssUrls[0] contains the weather url for London because if I increment the array $cityWeatherRssUrls[1] I receive the a message saying that the feed cannot be found.
Any thoughts?
Thanks in advance
There were quite a few key concepts missing from your code, but hopefully the following will help you fix your code…