PHP and XML XSLT Mysql combined use of the implementation code

  • 2020-03-31 16:42:56
  • OfStack

Using XML and XSLT in PHP USES some DLL libraries,
Extension =php_domxml.dll // function library that operates on XML
Extension =php_iconv.dll // transcoding, such as converting GB2312 to utf-8
Extension =php_xslt. DLL // function library for XSLT
When using the above three libraries, you need to add the DLLS directory in the PHP installation directory to the path, otherwise it will not be found
Of these libraries
In the following courses, we will use the PEAR class library in PHP, mainly using the DB class library, Sql2XML class library, you can in
Download the latest version from pear.php.net
After downloading the above two libraries, you'd better set it up in php.ini
Include_path = "); D: phppear."
D :phppear is the installation path for my pear
After setting up, restart the machine, otherwise because the path cannot be found, Apache will not start properly, can not be used normally

The setup to use XML and XSLT in PHP is now complete, isn't it simple :)
For users of Linux
http://www.gingerall.com/ to download the source code about XSLt
http://www.gnu.org/software/libiconv/ to download the source about the Iconv
Ii. PHP and XML, XSLT, Mysql combined application, preliminary
I'm going to talk about a simple process of extracting data from a database, generating an XML document, and using XSLT to transform it into HTML,
This process is only about using this technique and does not involve other techniques such as paging
For this example, I use the following database tables and data
Table name: enterprise user information table, English name: yhxx
Table structure:
#
The structure of # data table 'yhxx'
#
The CREATE TABLE yhxx (
NSRNM varchar(15) NOT NULL default ",
Qymc varchar(200) NOT NULL default '',
Qydh varchar(50) NOT NULL default ',
PRIMARY KEY (NSRNM)
) TYPE=MyISAM COMMENT=' user information table ';
#
Data table contents' yhxx '
#
INSERT INTO yhxx VALUES (' 310109040111985 ', 'qiaojia diet development co., LTD.', '8621-63346626');
INSERT INTO yhxx VALUES (' 310104040221736 ', 'Shanghai jialing trading co., LTD.', '74292546');
INSERT INTO yhxx VALUES (' 310108040331576 ', 'jade art company', '54861465');

Next, I started to write a program to extract data and a page to display it
For all to see, I use the easiest way to write a program
Program file name: browesdata.php
Page file name: browesdata.html
Procedures and page file download, the procedure has been in win2000, mysql through the test
See the code on the next page

If you're interested in this technology, let me show you a little bit more about how PHP works with XML, XSLT, and databases
 
<?php 
require_once "DB.php"; //Database processing classes in PEAR
$dataType = "mysql" ; //Database type
$user = "root"; //The user name
$pass = "abcd" ; //password
$host="202.96.215.200"; //Mysql database server address
$db_name = "test"; //The database name
$dsn="$dataType://$user:$pass@$host/$db_name"; // database-connected DNS preparation  
$db = DB::connect($dsn); //Connect to database
if (DB::isError($db)) 
{ 
die ($db->getMessage()); //Connection failed, output error message
} 
//The next two are public functions
 
function readXsl($filename) 
{ 
if(false==file_exists($filename)) 
{ 
echo " The file to read <font color='red'>$filename</font> There is no </br />"; 
return false ; 
} 
return implode('', file($filename)); 
} //end function readXsl 
/** 
*  will xml File or array variables according to xsl File conversion to HTML content  
* http://knowsky.com 
* @param array $arydata -  An array variable  
* @param String $xslstring - xsl Document data  
* @param String $xmlstring - xml Document data  
*/ 
function getHtml($arydata = false, $xslstring = false, $xmlstring = false) 
{ 
global $db ; //Use the $db object
include_once("XML/sql2xml.php"); //Include sql2xml
$sql2xmlclass = new xml_sql2xml($db); //Instantiate sql2xml
$sql2xmlclass->setEncoding("GB2312"); //Sets the transcode type of the data
if (false == $xmlstring) { //If the user passes in array data, the array data is applied to XSL
//Sets the name of the node that generates the XML document data
$options = array ( tagNameRow => "row" , 
tagNameResult => "result" 
); 
$sql2xmlclass->SetOptions($options); 
//Add the data to generate the XML document
$sql2xmlclass->add($arydata); 
} 
//Get the XML document
$xmlstring = $sql2xmlclass->getxml(); 
//print $xmlstring; 
//Let's begin transforming the XML data document into an HTML document using XSLT
$arguments = array('/_xml' => $xmlstring, 
'/_xsl' => $xslstring 
); 
$xh = xslt_create(); 
$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', null, $arguments); 
if ($result) { 
return $result; 
xslt_free($xh); 
} else { 
return " conversion xml Data to the xsl An error occurred when "; 
xslt_free($xh); 
} 
} //end function getHtml() 


//SQL statements that query data from the user information table
$sql = "select 
nsrnm, # code  
qymc, # An enterprise name  
qydh # The phone  
from 
yhxx # User information sheet "; 
//Execute SQL statement
$res = $db->query($sql); 
if ($db->isError($res)) 
{ 
echo " perform SQL Statement error "; 
} 
while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) 
{ 
$data[] = $row; //Put the data into an array
} 
//print_r($data); 
//You can see that the data is already in a multidimensional array
//Now that our program is almost complete, we'll define the page that displays the data
//Open your DW or FrontPage XP, make a display of the page, I made a, and for everyone to download
//We made the data display page file: browesdata.html
/* 
 This is the data list interface that we normally display  
<html> 
<head> 
<meta http-equiv="Content-Language" content="zh-cn"> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title> Data browsing </title> 
</head> 
<body> 
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> 
<tr> 
<td width="21%" align="center" bgcolor="#C0C0C0"> code </td> 
<td width="50%" align="center" bgcolor="#C0C0C0"> An enterprise name </td> 
<td width="29%" align="center" bgcolor="#C0C0C0"> The phone </td> 
</tr> 
<tr> 
<td width="21%"> </td> 
<td width="50%"> </td> 
<td width="29%"> </td> 
</tr> 
</table> 
</body> 
</html> 

 
*/ 
//I processed it into an HTML document in XSLT format
/* 
<?xml version="1.0" encoding="gb2312"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" version="1.0" encoding="GB2312" indent="yes" /> 
<xsl:template match="/"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
<title> Data browsing </title> 
</head> 
<body> 
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> 
<tbody> 
<tr> 
<td width="21%" align="center" bgcolor="#C0C0C0"> code </td> 
<td width="50%" align="center" bgcolor="#C0C0C0"> An enterprise name </td> 
<td width="29%" align="center" bgcolor="#C0C0C0"> The phone </td> 
</tr> 
<xsl:for-each select="root/result/row"> 
<tr> 
<td width="21%"> <xsl:value-of select="nsrnm" /></td> 
<td width="50%"> <xsl:value-of select="qymc" /></td> 
<td width="29%"> <xsl:value-of select="qydh" /></td> 
</tr> 

</xsl:for-each> 

</tbody> 

</table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

*/ 
$htmlFile="browesData.html" ; 
$htmlStr = readXsl($htmlFile); //Reads an HTML document in XSLT format into a variable
echo getHtml($data, $htmlStr) ; 
//End of the program
?> 

Related articles: