AJAX In Action [233]
header("Content-type: text/xml"); b
Declare mime type
echo("\n");
$db = mysql_connect("localhost","ajax","action"); c Connect to
mysql_select_db("ajax",$db);
database
$result = mysql_query("SELECT * FROM Contacts WHERE ContactName like '%".
$_GET['q']."%'",$db); d
Populate query
?>
if ($myrow = mysql_fetch_array($result)) { e Test results do { f Iterate through results ?> }while ($myrow = mysql_fetch_array($result)); }else{ ?> Show empty dataset } ?>
Licensed to jonathan zheng The server-side code: PHP 479 In order for this dynamic XML document generation to work, we must set the document’s type to text/xml b; if we skip this step, the XSLT transformation may not take place, especially with Mozilla and Firefox. The data that we are searching for is stored in a database table. We need to select the relevant entries. In this case, we are using PHP’s built-in MySQL functions to talk to the database directly, in order to keep things as simple as possible. We connect to the database ajax running on the local database server as the user ajax with password action c. We then construct our SQL query string using the request parameter passed in from the client code to populate the WHERE clause d. For a more robust server-side implementation, we recommend an ObjectRelational Mapping system such as Pear DB_DataObject (see chapter 3) rather than talking directly to the database as we have done here. However, the current implementation is simple and can be easily configured by readers wanting to test the example for themselves. Having returned the result set, we check whether it is empty e, and then either iterate over it f to create the phone entries, or print out the “No Results” message g. 12.3.2 Building the XSLT document We can use XSLT to transform our XML file into a nice HTML table with only a few lines of code. The XSLT document allows for pattern matching if necessary to display the data in any format we want. The pattern matching uses a template to display the data. We loop through the source tree nodes with the XSLT to display the data. The XSLT takes the structured XML file and converts it into a viewable format that is easy to update and change. Our XSLT document will be defined statically. Explaining the XSLT structure An XSLT transformation contains the rules for transforming a source tree into a result tree. The whole XSLT process consists of pattern matching. When a pattern is matched against the source tree elements, the template then creates our result tree. The result tree structure does not have to be related to the source tree structure. Since they can be different, we can take our XML file and convert it into any format we want. We are not required to stick with a tabular dataset. This XSLT transformation is called a stylesheet since it styles the result tree. The stylesheet contains template rules, which have two parts. The first part is the pattern, which is matched against the nodes of the source tree. When a match is Licensed to jonathan zheng 480 CHAPTER 12 Live search using XSLT found, the XSLT processor uses the second part, the template, which contains the tags to build the source