CHAPTER 13 CASE STUDY: (Managed web hosting) USING PHP FOR
CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 411 As the application shouldn t store the outdated weather entries in the database, the code uses the $weatherWindow variable to delete the old records. That way, the code can just select the remaining records: $sql = ‘DELETE FROM weather WHERE weatherCityID=’ . $city . ‘ . AND weatherDate < ' . $weatherWindow; mysql_query($sql) or die(mysql_error() . "n
” . $sql); The application also determines the forecast, based on how many people select each weather type. If 10 people indicate that the weather is sunny, and one person adds that it s raining, the application can probably assume that the weather is sunny. It could extend the logic and analyze weather changes over time, but that s beyond the scope of this application. The application determines weather type by counting the number of each type of entry: $sql = ‘SELECT count(weather.weatherWeatherTypeID) AS tOrder, . weathertype.weatherType FROM weather, weathertype . WHERE weatherWeatherTypeID=weatherTypeID and weatherCityID =’ . $city . ‘ . GROUP BY weatherWeatherTypeID ORDER BY tOrder DESC’; $wres = mysql_query($sql) or die(mysql_error() . “n
” . $sql); If the query returns no records, there are no current weather reports. It doesn t need to add any weather data to the XML document. It will only proceed if there are more than zero rows of data: if (mysql_num_rows($wres) > 0) { $wrow = mysql_fetch_array($wres); The code retrieves the minimum and maximum values by averaging the temperatures. It rounds the averaged value to display a whole number: $sql = ‘SELECT ROUND(AVG(weatherMax)) AS maxavg FROM weather . WHERE weatherCityID =’ . $city; $wMaxRes = mysql_query($sql) or die(mysql_error() . “n
” . $sql); $wMaxRow = mysql_fetch_array($wMaxRes); $sql = ‘SELECT ROUND(AVG(weatherMin)) AS minavg FROM weather . WHERE weatherCityID =’ . $city; $wMinRes = mysql_query($sql) or die(mysql_error() . “n
” . $sql); $wMinRow = mysql_fetch_array($wMinRes); The page needs to add these elements to the XML document: $temp = $xml->createElement(’temperature’); $temp = $root->appendChild($temp); $min = $xml->createElement(’minimum’, $wMinRow[’minavg’]); $min = $temp->appendChild($min); $max = $xml->createElement(’maximum’, $wMaxRow[’maxavg’]); $max = $temp->appendChild($max);
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.