I've created a website to display weather and currency rates for two cites; London and New York. The data comes from XML feeds from Yahoo Weather and the Money Converter.
Solution 1
I have a single configuration file to store variables and arrays for both cities.
My current solution is the following for my configuration file cityConfig.php:
<?php // 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); ?>
Solution 2
Another alternative I come up with was to rename the variables for each city and adding them all to an array at the end of this script but this means declaring 'poor' variable names like:
// City 1 // $city1 = 'London'; $currencyRateHeading1 = '1 British Pound Sterling';
Basically adding a 1 at the end of the variable name to define city 1 or city 2. Any recommendations for optimising this file and placing values into an array? I'm trying to develop a better understanding.
I don't really want to go down the road of using databases like MySQL.
I call the arrays in my threeColumnContainer.php script depending on which page the user is viewing by setting a $current variable.
<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>

New Topic/Question
Reply



MultiQuote




|