Hey group,

Just recently I got some help from you to do (what seems fairly simple now) to put together an XML with XSLT. That's all well and good, but I have tried to put in some PHP code in the middle of the xml, to loop through entries of a database and dynamically add nodes.

To my slight disappointment, I see it is not as easy as this - so, off my research path I went.

I'm a little confused because I am using PHP 5.2.5, and in phpinfo(), DOM/XML is enabled. Yet by trying to use the function domxml_open_file(), I get:

Fatal error: Call to undefined function domxml_open_file()

I understand that these functions have been moved to the PECL repository, so what can I get my host server to do to get this to work?

Either that, or SimpleXML being enabled - is there similar functions I can use to perform the task of transforming the xml from an xslt?

For the sake of sharing, this is the code I have quickly wrote up:

Code:
<?php
// Load the xml/php file to operate on
$filename = 'treeview_data.xml';

// Prepare initial data to write:
$header_data = '<?xml version="1.0" encoding="ISO-8859-1"?>' . "/n";
$header_data .= '<?xml-stylesheet type="text/xsl" href="plugins/treeview/treeview.xslt"?>' . "/n";
$header_data .= '<treeview title="Test Explorer Win">' . "/n";
$header_data .= '<folder title="Desktop" img="desktop.gif" alt="test alt text" code="Desktop" expanded="true">' . "/n";
$header_data .= '<folder title="My Computer" img="computer.gif" alt="test alt text" code="MyComputer" expanded="true">' . "/n";

$footer_data = '<folder title="Control Panel" img="config.gif" alt="test alt text" code="config" expanded="false"/>' . "/n";
$footer_data .=	'</folder>' . "/n";
$footer_data .=	'</folder>' . "/n";
$footer_data .=	'<folder title="My Documents" img="MyDocuments.gif" alt="test alt text" code="mydocs" expanded="false">' . "/n";
$footer_data .=	'<folder title="My Pictures" img="MyImages.gif" alt="test alt text" code="myimages" expanded="false"/>' . "/n";
$footer_data .=	'</folder>' . "/n";
$footer_data .=	'<folder title="My Network Places" img="network.gif" alt="test alt text" code="network" expanded="false"/>' . "/n";
$footer_data .=	'<folder title="Recycle Bin" img="trash.gif" alt="test alt text" code="trash" expanded="false"/>' . "/n";
$footer_data .=	'</treeview>' . "/n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'w')) {
         echo "Cannot open file ($filename)";
         exit;
    }
    
    // Write the header data to our opened file.
    if (fwrite($handle, $header_data) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    
    //echo "Success, wrote ($somecontent) to file ($filename)";

    // List drives
	include 'dblinks.php';
	mysql_select_db("dbname");
	$query = "SELECT * FROM drives WHERE user_id='" . $_SESSION["user_id"] . "'";
	$result = mysql_query($query, $db );
	if (!$result) {
		die('Invalid query: ' . mysql_error());	
		echo 'E_FAIL_DB';
	}
	$num = mysql_num_rows($result);
	if ($num > 0) {
		while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    		fwrite($handle, '<folder title="' . $row["label"] . '" code="Drive::' . $row["id"] . '" img="harddrive.gif"/>');
		}
	}
    
    // Write the footer data to our opened file.
    fwrite($handle, $footer_data);

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}

$filename = "plugins/treeview/treeview.xslt";
$xmldoc = domxml_open_file("treeview_data.xml");
$xsldoc = domxml_xslt_stylesheet_file($filename);
$result =  $xsldoc->process($xmldoc);
$xsldoc->result_dump_file($result, "treeview.xml"); 
include 'treeview.xml';    
?>
Cheers,
Jess.