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

January 17th, 2008

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)

January 16th, 2008

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)

January 16th, 2008

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

January 15th, 2008

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.

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

January 15th, 2008

402 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION The page then repeats the process for continents: else if (isset($continent)) { $sql = ‘SELECT * from continent WHERE continentID =’ . $continent; $tres = mysql_query($sql) or die(mysql_error()); 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 continent’); $error = $root->appendChild($error); } else { $trow = mysql_fetch_array($tres); $continent_name = $trow[’continent’]; $sql = ‘SELECT * FROM country WHERE countryContinentID =’ . $continent . . ‘ ORDER BY country’; $cres = mysql_query($sql) or die(mysql_error()); $current = $xml->createElement(’current’, $continent_name); $current->setAttribute(’type’, ‘continent’); $current->setAttribute(’id’, $continent); $root->appendChild($current); $items = $xml->createElement(’items’); $root->appendChild($items); $linksto = $xml->createElement(’linksto’, ‘country’); $items->appendChild($linksto); while ($crow = mysql_fetch_array($cres)) { $entry = $xml->createElement(’entry’, $crow[’country’]); $entry->setAttribute(’id’, $crow[’countryID’]); $items->appendChild($entry); } } } If none of the variables has been set, users are at the top level of navigation, and the application must display a list of continents from the database: else { $sql = ‘SELECT * FROM continent ORDER BY continent’; $cres = mysql_query($sql) or die(mysql_error()); $current = $xml->createElement(’current’, ‘Home’); $current->setAttribute(’type’, ‘home’); $root->appendChild($current); $items = $xml->createElement(’items’); $root->appendChild($items); $linksto = $xml->createElement(’linksto’, ‘continent’); $items->appendChild($linksto); while ($crow = mysql_fetch_array($cres)) {
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Web hosting uk - CHAPTER 13 CASE STUDY: USING PHP FOR

January 14th, 2008

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 401 Finally, it loops through the results and creates a set of elements. Obviously, if there are no cities, it doesn t create any elements: while ($crow = mysql_fetch_array($cres)) { $entry = $xml->createElement(’entry’, $crow[’city’]); $entry->setAttribute(’id’, $crow[’cityID’]); $items->appendChild($entry); } Note that I ve left out the closing brackets to simplify the code. The code repeats this process for the country. This time, it returns the areas with the country as elements instead of the cities: else if (isset($country)) { $sql = ‘SELECT * from country WHERE countryID =’ . $country; $tres = mysql_query($sql) or die(mysql_error()); 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 country’); $error = $root->appendChild($error); } else { $trow = mysql_fetch_array($tres); $country_name = $trow[’country’]; $sql = ‘SELECT * FROM area WHERE areaCountryID =’ . $country . ‘ . ORDER BY area’; $cres = mysql_query($sql) or die(mysql_error()); $current = $xml->createElement(’current’, $country_name); $current->setAttribute(’type’, ‘country’); $current->setAttribute(’id’, $country); $root->appendChild($current); $items = $xml->createElement(’items’); $root->appendChild($items); $linksto = $xml->createElement(’linksto’, ‘area’); $items->appendChild($linksto); while ($crow = mysql_fetch_array($cres)) { $entry = $xml->createElement(’entry’, $crow[’area’]); $entry->setAttribute(’id’, $crow[’areaID’]); $items->appendChild($entry); } } }
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

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

January 14th, 2008

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.

Yahoo free web hosting - CHAPTER 13 CASE STUDY: USING PHP FOR

January 13th, 2008

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 399 If users change the URL to http://localhost/weather/index.php?continent=7643, one of two things can happen. Either the value 7643 refers to a continent in the database, or the id is invalid. In the first case, the application can display the subnavigation for that continent, but in the second case, it needs to display an error message. The application uses the following XML document structure for this scenario: Error You appear to have selected an invalid continent Instead of an element, the document includes Error as the value of the element. It also includes an element with an error message that displays to the user. Scenario 3: Dealing with Subnavigation Items The most likely scenario is that the application displays subnavigation items. The structure of this type of XML document is similar to that shown in scenario 1. The difference is that the element contains multiple elements: WA
city Albany Bunbury Geraldton Perth
Each element has an idthat corresponds to the idfield in the database. It also contains the name of the navigation item in this case, the name of the city. Now let s see how to build the XML document to cope with these different scenarios. Building the XML Document The mk_navxml.php document starts by including weather.php: xmlStandalone = false;
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

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

January 13th, 2008

398 CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION The application uses the following template for the XML document built from the database content:
The document element is . The element specifies the user s current position in the navigation system. The element contains a
element. This element specifies the links from this level. The element also contains a list of elements, one for each navigation item at this level. An XSLT stylesheet uses the values in the elements to create the links to the subsequent levels of navigation. The structure of the XML document built by the application needs to take into account the following scenarios: 1. There are no subnavigation items to display. 2. The values in the querystring change and cause an error. 3. There are subnavigation items to display. The last scenario is the most likely, but I ll look at the XML structure that the application needs to produce for each option. Scenario 1: No Subnavigation Items The first situation I ll look at is where the users have reached a point in the navigation where there are no subnavigation items. A sample XML file structure for this scenario follows: WA
city
In this structure, I ve navigated to Western Australia (WA), a state in Australia. The element shows that I m in an areacalled WA. However, the element doesn t contain tags, as no cities are specified within the area. Scenario 2: Changing Querystring Variables Users might change the values in the querystring, perhaps changing one of the variables to see what happens. Let s assume that the URL to generate the XML document in scenario 1 was http://localhost/weather/index.php?continent=1.
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 (Web hosting comparison) FOR

January 12th, 2008

CHAPTER 13 CASE STUDY: USING PHP FOR AN XML APPLICATION 397 Let s work through the code in a little more detail. First, the page creates two DomDocument objects for the XML and XSL documents: $xsl = new DomDocument(); $inputdom = new DomDocument(); Then it tests to see if the $city variable is set and includes the appropriate document. The code also loads the related XSLT stylesheet: if (isset($city)) { include ‘mk_weather.php’; $xsl->load(’weather.xsl’); } else { include ‘mk_navxml.php’; $xsl->load(’nav.xsl’); } The next code block loads the XML content and creates a new XsltProcessor: $xml->loadXML($xml->saveXML()); $proc = new XsltProcessor(); The code then imports the stylesheet and applies the transformation, displaying the output in the page: $xsl = $proc->importStylesheet($xsl); $newdom = $proc->transformToDoc($xml); echo $newdom->saveXML(); mk_navxml.php The mk_navxml.php script is a complicated page responsible for much of the work in the application. This page creates the XML document that the application uses for the navigation in the site. The variables passed to the page determine the navigation system. There are four types of navigation to display: 1. The $area variable is set, so the navigation should display the cities. 2. The $country variable is set, so the navigation should display the areas. 3. The $continent variable is set, so the navigation should display the countries. 4. No variables are set, so the navigation should display a list of continents.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.