PHP Version

In this version, the resources listed under the Tools tab of http://www.thexmltoolkit.org are stored in a mySQL database, rather than in an XML file. The PHP script establishes the connection with the database, checks the connection, performs the query, and returns the results to be combined with a static XML file, transformed by the XSL, and formatted with CSS to display the Web page.

The resulting Web page looks exactly like the Tools page on this Web site; the only difference being that the names, descriptions, and URLs of the listed resources come from a database.


<?php 
/*******************************************************************************************
* COPYRIGHT NOTICE:
* Copyright 2006 Center for Technology in Government.
* The Center grants permission to use and modify this software code free of charge provided  
* this copyright notice is included. By using this code you agree to indemnify the Center from 
* any liability that might arise from its use. Selling the code is expressly forbidden.
*******************************************************************************************/

/*************DATABASE CONNECTION INFO*********/
$hostname="localhost";
$mysql_login="loginname";
$mysql_password="loginpassword";
$database="databasename";
$table_id = "tablename";
/*********************************************/

/***********Connect to database***************/
if(!($dbconnect = mysql_connect($hostname, $mysql_login , $mysql_password)))
{
   echo "Connection failed to the host 'localhost'.";
   exit;
} 

if (!(mysql_select_db("$database",$dbconnect))) 
{
   echo "Cannot connect to database $database";
   exit;
} 
/********************************************/

/****************Send Database Query*********/
$query = "SELECT * FROM $table_id";
$dbresult = mysql_query($query, $dbconnect);
/*********************************************/

/*********************************************
 * Open XML file, only has title info, no data
 * Get data from database 
 * Convert database data to XML format, 
 * Append new data to XML file, processed only in memory 
 * Generated xml data format must conform to existing XSL sheet
 * Use existing XSL file, nothing needs to be changed
 * New xml data only exist in memory 
**************************************************/

$dom =  new DomDocument('1.0');
// Load XML file, this xml file only have title infor, can not be changed.
$dom->load("tools/tools_title.xml");
// Create Instance of DomXPath
$xpath = new DomXPath($dom);
// new node will be inserted as child of this node
$next = $xpath->query("//book/chapter");
$next = $next->item(0); 

while ($row = mysql_fetch_array($dbresult,MYSQL_ASSOC)) 
{
  // Create the new element
  $sect1 = $dom->createElement('sect1');
  // Insert the new element
  $sect1 = $next->appendChild($sect1);     
    $title = $dom->createElement('title');
    $title = $sect1->appendChild($title);
        $ulink = $dom->createElement('ulink');
        $ulink = $title->appendChild($ulink);
        $ulink->setAttribute('url', $row{'url'});
             $value = $dom->createTextNode($row{'title'});
             $value = $ulink->appendChild($value);
    $para = $dom->createElement('para');
    $para = $sect1->appendChild($para);
         $value = $dom->createTextNode($row{'description'}."(");
         $value = $para->appendChild($value);
         $ulink = $dom->createElement('ulink');
         $ulink = $para->appendChild($ulink);
         $ulink->setAttribute('url', $row{'url'});
             $value = $dom->createTextNode($row{'url'});
             $value = $ulink->appendChild($value);
         $value = $dom->createTextNode(")");
         $value = $para->appendChild($value);      
}
//Close database connection
mysql_close($dbconnect);
/*************************************************************************/

/*****************************************
 * Translate xml data into html 
 * Send back to clent browser
 * Use existing XSL file 
 * Processed in Memory 
*****************************************/

$xsl = new DomDocument();
$xsl->load("level1.xsl");
//create the processor and import the stylesheet
$proc = new XsltProcessor();
$xsl = $proc->importStylesheet($xsl);
$proc->setParameter(null, "name", "tools"); 
$proc->setParameter(null, "extension", ".php"); 
//transform and output the xml document
$newdom = $proc->transformToDoc($dom);
print $newdom->saveXML();

?> 

Valid values would need to be inserted for your database connection. Otherwise, this script works much like the scripts in the Environment section of this Web site.