410 CHAPTER 13 CASE (Web site design) STUDY: USING PHP
410 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION Building the XML Document The mk_weather.php script starts by including the weather.php page and creating a new DomDocument: element: $xml->xmlStandalone = false; $root = $xml->createElement(’weather’); $root = $xml->appendChild($root); The page needs to query the database to find out the city name. This code also tests whether users have passed in a valid id for the city: $sql = ‘SELECT * FROM city WHERE cityID =’ . $city; $cres = mysql_query($sql) or die(mysql_error() . “n
” . $sql); If the idis not valid, the code generates an error: if (mysql_num_rows($cres) == 0) { $cityElement = $xml->createElement(’city’, ‘Error’); $root->appendChild($cityElement); $error = $xml->createElement(’error’, ‘You appear to have selected . an invalid city’); $root->appendChild($error); } If the application has a valid city id, it retrieves the name of the city and adds it to the XML document: else { $crow = mysql_fetch_array($cres); $city_name = $crow[’city’]; $cityElement = $xml->createElement(’city’, $city_name); $cityElement->setAttribute(’id’, $city); $cityElement = $root->appendChild($cityElement); The code uses the createElement(), setAttribute(), and appendChild() methods to add the content. Because the application should only show the current weather reports, you can filter the details to show only current entries. In this application, entries added in the last eight hours are current. The variable $weatherWindow has a value of the current time minus eight hours, or 28800 seconds: $weatherWindow = time() - 28800;
Check Tomcat Web Hosting services for best quality webspace to host your web application.