400 CHAPTER 13 CASE STUDY: USING PHP (Web hosting account)

400 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION The code then sets the document element using the createElement() and appendChild() methods: $root = $xml->createElement(’entries’); $root = $xml->appendChild($root); The next code block tests which variable has been set: $area, $country, or $continent. It does this in an if/else statement that starts with the lowest navigation level, area: if (isset($area)) { If the $city variable is set, the code in standard.php will branch to include the mk_weather.php script instead. The code starts by retrieving the current area information from the database: $sql = ‘SELECT * from area WHERE areaID=’ . $area; $tres = mysql_query($sql) or die(mysql_error()); It also tests that the area id is valid: if (mysql_num_rows($tres) == 0) { $current = $xml->createElement(’current’, ‘Error’); $current = $root->appendChild($current); $error = $xml->createElement(’error’, ‘You appear to have selected . an invalid area’); $error = $root->appendChild($error); } If the query returns no rows, the application knows that the area id is invalid and the page can generate an element. If the query returns rows, the area name is stored in a variable for later use: else { $row = mysql_fetch_array($tres); $area_name = $row[’area’]; Once the page determines the area, it then needs to select the subnavigation city items: $sql = ‘SELECT * FROM city WHERE cityAreaID =’ . $area . ‘ ORDER BY city’; $cres = mysql_query($sql) or die(mysql_error()); The code can then use DOM methods to create the element and set the attributes: $current = $xml->createElement(’current’, $area_name); $current->setAttribute(’type’, ‘area’); $current->setAttribute(’id’, $area); $root->appendChild($current); It also creates the and
elements: $items = $xml->createElement(’items’); $root->appendChild($items); $linksto = $xml->createElement(’linksto’, ‘city’); $items->appendChild($linksto);
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Leave a Reply