I've created a website that displays weather and currency rates for London and New York. The following is an abstract from my cityConfig.php
script. Is this a good approach for adding variables from each city into an array?
// Define arrays //
$cities = array();
$currencyRateHeadings = array();
$currencySource = array();
$weatherSource = array();
// Feed URL's //
$yahooWeather = 'http://weather.yahooapis.com/forecastrss?p=';
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';
// City 1 //
$city = 'London';
$currencyRateHeading = '1 British Pound Sterling';
$cityWeatherSourceCode = 'UKXX0085';
$cityWeatherSource = $yahooWeather . $cityWeatherSourceCode . '&u=c';
$currencyRateFeedCode = 'GBP';
$cityCurrencySource = $theMoneyConverter . $currencyRateFeedCode . '/rss.xml';
array_push($currencySource, $cityCurrencySource);
array_push($weatherSource, $cityWeatherSource);
array_push($cities, $city);
array_push($currencyRateHeadings, $currencyRateHeading);
// City 2 //
$city = 'New York';
$currencyRateHeading = '1 US Dollar';
$cityWeatherSourceCode = 'USNY0996';
$cityWeatherSource = $yahooWeather . $cityWeatherSourceCode . '&u=c';
$currencyRateFeedCode = 'USD';
$cityCurrencySource = $theMoneyConverter . $currencyRateFeedCode . '/rss.xml';
array_push($cities, $city);
array_push($currencyRateHeadings, $currencyRateHeading);
array_push($currencySource, $cityCurrencySource);
array_push($weatherSource, $cityWeatherSource);
Another alternative I considered was to create extra variables by adding either a 1 or a 2 to the end of the variable name (depending whether they are related to city1
or city2
) and push everything onto the array at the end.
// Define arrays //
$cities = array();
$currencyRateHeadings = array();
$currencySource = array();
$weatherSource = array();
// Feed URL's //
$yahooWeather = 'http://weather.yahooapis.com/forecastrss?p=';
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';
// City 1 //
$city1 = 'London';
$currencyRateHeading1 = '1 British Pound Sterling';
$cityWeatherSourceCode1 = 'UKXX0085';
$cityWeatherSource1 = $yahooWeather . $cityWeatherSourceCode . '&u=c';
$currencyRateFeedCode1 = 'GBP';
$cityCurrencySource1 = $theMoneyConverter . $currencyRateFeedCode . '/rss.xml';
// City 2 //
$city2 = 'New York';
$currencyRateHeading2 = '1 US Dollar';
$cityWeatherSourceCode2 = 'USNY0996';
$cityWeatherSource2 = $yahooWeather . $cityWeatherSourceCode . '&u=c';
$currencyRateFeedCode2 = 'USD';
$cityCurrencySource2 = $theMoneyConverter . $currencyRateFeedCode . '/rss.xml';
array_push($cities, $city1, $city2);
array_push($currencyRateHeadings, $currencyRateHeading1, $currencyRateHeading2);
array_push($currencySource, $cityCurrencySource1, $cityCurrencySource2);
array_push($weatherSource, $cityWeatherSource1, $cityWeatherSource2);
Can anyone provide an example of a better way of doing this?
The arrays and variables are called later in my threeColumnContainer.php
script after passing through the switch
statement to determine which page they have come from. I'm happy with what's happening below at the moment. It's just setting the array's that I'd like feedback for?
<li class="leftCol">
<p>
<?php
switch ($currentPage) {
case 'index.php':
echo displayCityContent($weatherSource[0], $columnSubheading);
break;
case 'currencyRate.php':
echo displayCurrencyRateContent($currencySource[0]);
break;
default:
content_unavailable();
break;
}
?>
</p>
</li>
<li class="rightCol">
<p>
<?php
switch ($currentPage) {
case 'index.php':
echo displayCityContent($weatherSource[1], $columnSubheading);
break;
case 'currencyRate.php':
echo displayCurrencyRateContent($currencySource[1]);
break;
default:
content_unavailable();
break;
}
?>
</p>
</li>
$cities = array(array('key'=>'first value'),array('key'=>'another value'));
) in an included file that setup your array's structure. \$\endgroup\$ – Jared Farrish Feb 4 '12 at 12:03