412 CHAPTER 13 CASE STUDY: USING PHP (Jetty web server)

412 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION The application also adds the outlook to the document. Because the code sorts the query in reverse count order, it displays the first record, which contains the highest number of responses: $outlook = $xml->createElement(‘outlook’, $wrow['weatherType']); $outlook = $root->appendChild($outlook); As the page finishes with the weather report, it can output the available weather types. You ve seen this code before: $types = $xml->createElement(‘weathertypes’); $types = $root->appendChild($types); $sql = ‘SELECT weatherTypeID, weatherType FROM weatherType’; $tRes = mysql_query($sql) or die(mysql_error() . “n
” . $sql); while ($tRow = mysql_fetch_array($tRes)) { $type = $xml->createElement(‘type’, $tRow['weatherType']); $type->setAttribute(‘id’, $tRow['weatherTypeID']); $type = $types->appendChild($type); } } weather.xsl The application needs to transform the XML content using the XSLT stylesheet weather.xsl. The stylesheet starts in the following way: It then checks that there are weather results by counting the number of temperature elements and storing the value in a variable: The value is 1 if users have entered a forecast, and 0 if there are no database results. The stylesheet can then test to see if an error occurred:

Error

If there is an error, the stylesheet displays the details; otherwise, it displays the weather title:

Weather for

Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

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.

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.

CHAPTER 13 CASE STUDY: USING PHP FOR (Web server hosting)

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 409 The page also contains a form that allows users to add a new weather report. To make life easier, the application provides a list of weather types in a drop-down list. This information comes from the element. Scenario 2: Changing Querystring Variables The second possibility occurs when users edit the querystring to add an invalid city code. This would produce the following XML document: Error You appear to have selected an invalid city This document provides users with an error message. Scenario 3: Current Weather Reports Available The final scenario shows a current weather report for the selected city. The XML document needs to include the current weather conditions with the possible weather types: Perth 20 35 hot hot sunny windy cloudy rain rainstorms snow snowstorms The element provides the minimum and maximum temperatures. The element is the current outlook for the city. It contains one of the predefined weather types. Now that you ve seen the XML document structures, I ll look at the code that builds these structures from the database.
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Web server version – 408 CHAPTER 13 CASE STUDY: USING PHP

408 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION Scenario 1: No Current Weather Reports In the first scenario, the selected city has no current weather reports. Figure 13-12 shows how this appears to users. Figure 13-12. There is no current weather report to display. The XML document describing the weather in this scenario would appear as follows: Perth hot sunny windy cloudy rain rainstorms snow snowstorms The element is the document element. This element includes the element, which contains the city name as text and an attribute with the id from the database.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Zeus web server – CHAPTER 13 CASE STUDY: USING PHP FOR

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 407 if (strlen($sql) > 0) { mysql_query($sql) or die(mysql_error()); } header(‘Location: index.php?’ . $current . ‘=’ . $parent); ?> The $current variable contains the previous navigation level, while $parent contains the id of that entry. I ve now worked through the code that builds the site navigation. The remainder of the application handles the weather details. The mk_weather.php, weather.xsl, and addweather.php scripts deal with the weather details. The application uses the same approach as it did with the navigation. The mk_weather.php script generates the weather XML, which weather.xsl transforms into XHTML. The addweather.php page allows users to add new weather details. mk_weather.php The mk_weather.php page generates the XML document containing current weather details. It uses the following template: hot There are three possibilities for the structure of the XML document that the application generates: 1. There is no current weather report. 2. The values in the querystring change and cause an error. 3. There is a current weather report. I ll work through each scenario.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Virtual web hosting – 406 CHAPTER 13 CASE STUDY: USING PHP

406 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION It then retrieves the details from the form, including the values in the hidden fields: $into = $_POST['into']; $current = $_POST['current']; $parent = $_POST['parent']; $entry = $_POST['entry']; The page tests to see that users have entered details into the form: if (strlen(trim($entry)) > 0) { If so, it uses the $into variable to determine the appropriate INSERT statement and stores it in the variable $sql: switch ($into) { case ‘continent’: $sql = ‘INSERT into continent (continent) . VALUES (“‘ . htmlspecialchars($entry,ENT_QUOTES) . ‘”)’; break; case ‘country’: $sql = ‘INSERT into country (country, countryContinentID) . VALUES (“‘ . htmlspecialchars($entry,ENT_QUOTES) . ‘”,’ . $parent . ‘)’; break; case ‘area’: $sql = ‘INSERT into area (area, areaCountryID) . VALUES (“‘ . htmlspecialchars($entry,ENT_QUOTES) . ‘”,’ . $parent . ‘)’; break; case ‘city’: $sql = ‘INSERT into city (city, cityAreaID) . VALUES (“‘ . htmlspecialchars($entry,ENT_QUOTES) . ‘”,’ . $parent . ‘)’; break; default: $sql = ”; break; } } else { $sql =”; } Finally, the code checks for a SQL statement, in which case the length of the $sql variable must be greater than 0. It then inserts the new record and redirects to the previous navigation position:
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

CHAPTER 13 CASE STUDY: USING PHP FOR (Apache web server for windows)

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 405 The remainder of the block creates the visible text field and Add button:

The form appears within the element, so it only displays if a valid record exists. Figure 13-11 shows the form as it appears when working at the city level. Figure 13-11. Adding a new city addnew.php If you add new details, the form action calls the addnew.php script. This script inserts the new record into the appropriate table in the database. I ll break down the page and discuss each section. To start with, the page includes weather.php to access the database: If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

404 CHAPTER 13 CASE STUDY: USING PHP (Web hosting)

404 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION If there are no links, it displays a message to that effect: There are currently no entries in the database under Otherwise, it displays the links, using the value of the $linksto variable to create the URL:

Please select a :

index.php?. =
This portion of the template generates XHTML similar to the following:

Please select a country:

Australia
New Zealand
After the stylesheet displays the subnavigation links, it displays a form that allows users to add a new entry at the current level:

Add a new :

The code needs to pass the level at which you re adding this entry, the id of the parent record, and the current navigation level. It does this using hidden form fields: <input type=”hidden” name=”current” value=” ” /> <input type=”hidden” name=”parent” value=” ” /> <input type=”hidden” name=”into” value=” ” /> The current level determines which table receives the new record. The code then inserts the parent id value into the record. The current navigation level redirects users to the current page after inserting the record.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Web site management – CHAPTER 13 CASE STUDY: USING PHP FOR

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 403 $entry = $xml->createElement(‘entry’, $crow['continent']); $entry->setAttribute(‘id’, $crow['continentID']); $items->appendChild($entry); } } By the time the script finishes running, it has built an XML document that is transformed in the standard.php script, as you saw earlier. nav.xsl Let s look at the XSLT stylesheet, nav.xsl in more detail. This stylesheet transforms the XML document from the mk_navxml.php page. The stylesheet starts with an XML declaration and the opening element: The code creates some XSLT variables to store values that the stylesheet will use: It uses the linksto variable to determine the link type, and the numLinks variable to determine whether any links exist. The page uses the element to provide some conditional logic. To start with, it identifies errors:

Error

When the application has an error, the stylesheet displays it in a level 4 heading. If there is no error, it displays the requested details:

Current:

The stylesheet uses another element to see if there are any subnavigation links:
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.