416 CHAPTER 13 CASE STUDY: USING PHP (Web site translator)

416 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION Summary In this chapter, I worked through an application that uses PHP, MySQL, XML, and XSLT to display and manage weather content. The application stores all of the data within a MySQL database. The application retrieves the relevant database records with PHP 5. It uses the new PHP 5 DomDocument object to generate the XML document. The structure of the generated XML documents is flexible enough to cope with several different scenarios. In order to display the XML content within the application, I used XSLT stylesheet transformations to generate XHTML. You saw how to use XSLT variables and include conditional logic in the stylesheets. This chapter wraps up the book. I hope you ve enjoyed reading about XML and that you ve expanded your knowledge. XML is a flexible approach to building both client- and server-side web applications, and I hope the contents of this book will make you as enthusiastic about XML as I am!
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

CHAPTER 13 CASE STUDY: USING PHP FOR (1 on 1 web hosting)

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 415 addweather.php The final part of the application processes the weather details entered by users and adds the content to the database. As I mentioned earlier, the database stores all values in Celsius degrees. The page addweather.php starts with a conversion function that converts Fahrenheit temperatures to Celsius: Figure 13-7 earlier in the book shows how the completed application appears when viewing the weather for a city.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Web host server – 414 CHAPTER 13 CASE STUDY: USING PHP


The stylesheet also includes a form so users can add a new weather report:

Add a new entry:

The form needs to pass the current city id, so the application can store the value in the database: <input type=”hidden” name=”city” value=” ” /> The rest of the form provides appropriate inputs as well as a drop-down list showing the different weather types: Temperature is in:

We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

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

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 413 The stylesheet needs to check the value of the $numTemp variable. If the value is 0, there are no weather records, and it will display an appropriate message: There are currently no entries for If the value isn t 0, there are weather details that the stylesheet can display. As I m writing this from Australia, I use the Celsius temperature scale. The database stores the temperatures in Celsius, but the application needs to display both Celsius and Fahrenheit values. The stylesheet converts the existing Celsius temperatures to Fahrenheit values and stores them in variables: The stylesheet displays the weather outlook using images designed by Gavin Cromhout. You can find them in the imagesfolder. It chooses the images in the following way: Outlook:
<img src=”images/ .jpg” width=”100″ height=”80″ . alt=” ” />
It then displays the minimum and maximum temperatures in a table:

You want to have a cheap webhost for your apache application, then check apache web hosting services.

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:


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.


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.