Combining PHP with XML, XSLT, and MySQL to implement code _php skills

Source: Internet
Author: User
Tags dsn pear xsl xsl file xslt
Use XML and XSLT in PHP to use some DLL libraries,
Extension=php_domxml.dll//Operation XML Library of functions
Extension=php_iconv.dll//transcoding, such as converting GB2312 to UTF-8
function Library of Extension=php_xslt.dll//XSLT
When using the three libraries above, you should add the DLLs directory under the PHP installation directory to the path, or you will not be able to find
Of these class libraries
In the following course, we will use the Pear Class Library in PHP, mainly with the DB Class Library, Sql2xml class library, you can
Pear.php.net Download the latest version
After downloading the above two class libraries, we'd better set up in php.ini
include_path = ".; D:phppear "
D:phppear is the installation path of my pear
After setting up, restart the machine, or because the path can not be found to cause the Apache boot is not normal to use

This is done by using the XML and XSLT settings in PHP, which is simple:
Users who use Linux can go to the
http://www.gingerall.com/Download the source code for XSLT
http://www.gnu.org/software/libiconv/Download the source code for ICONV
Two PHP with XML, XSLT, MySQL, the combination of application, the first chapter
What I'm going to talk about here is a simple process of extracting data from a database, generating an XML document, and translating it into HTML using XSLT,
This process only speaks of the process of using this technology and does not involve paging and other technologies
This example I use the following database tables and data
Table Name: Enterprise User Information table, English name: yhxx
Table structure:
#
# The structure of the datasheet ' yhxx '
#
CREATE TABLE Yhxx (
NSRNM varchar not NULL default ',
QYMC varchar not NULL default ',
QYDH varchar not NULL default ',
PRIMARY KEY (NSRNM)
) Type=myisam comment= ' User Information table ';
#
# datasheet content ' Yhxx '
#
INSERT into Yhxx VALUES (' 310109040111985 ', ' Joe's Diet Development Co., Ltd. ', ' 8621-63346626 ');
INSERT into Yhxx VALUES (' 310104040221736 ', ' Shanghai Jialing Trade Co., ltd ', ' 74292546 ');
INSERT into Yhxx VALUES (' 310108040331576 ', ' Jade Art Company ', ' 54861465 ');

Next I start to write the data extraction program and the page that displays the data
In order for everyone to understand, I use the simplest way to write a program
program file name: browesdata.php
Paging File name: browesdata.html
Procedures and page files to provide downloads, this program has been in Win2000, MySQL passed the test
Code See next page

If you're interested in this technology, I'll tell you more about PHP and XML, XSLT, and databases. A further application
code as follows:

<?php
Require_once "db.php"; Database processing classes in Pear
$dataType = "MySQL"; Database type
$user = "root"; User name
$pass = "ABCD"; Password
$host = "202.96.215.200"; MySQL Database server address
$db _name = "Test"; Database name
$DSN = "$dataType://$user: $pass @ $host/$db _name"; DNS Configuration of connection database
$db = Db::connect ($DSN); Connecting to a database
if (Db::iserror ($DB))
{
Die ($db->getmessage ()); Connection failed, output error message
}
The following two are public functions
/**
* Read the XSL document
*
* @param String $filename The name of the-xsl file
* @return String
*/
function readxsl ($filename)
{
if (false==file_exists ($filename))
{
echo "to read the file <font color= ' red ' > $filename </font> does not exist </br/>";
return false;
}
Return implode (', File ($filename));
}//end function readxsl
/**
* Convert an XML file or an array variable from an XSL file to an HTML content
* http://knowsky.com
* @param array $arydata-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 just the $db object
Include_once ("xml/sql2xml.php"); To include Sql2xml in.
$sql 2xmlclass = new Xml_sql2xml ($DB); Instantiate the Sql2xml
$sql 2xmlclass->setencoding ("GB2312"); Set the type of transcoding for data
if (false = = $xmlstring) {//If the user passes in the array data, the array data is applied to the XSL
Set the node name for generating XML document data
$options = Array (tagnamerow => "Row",
Tagnameresult => "Result"
);
$sql 2xmlclass->setoptions ($options);
Add data to generate an XML document
$sql 2xmlclass->add ($arydata);
}
Get the XML document
$xmlstring = $sql 2xmlclass->getxml ();
Print $xmlstring;
The following begins converting an 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 "error converting XML data to XSL";
Xslt_free ($XH);
}
}//end function gethtml ()


SQL statement that queries data from the User information table
$sql = "Select
NSRNM, #代码
QYMC, #企业名称
Qydh #电话
From
Yhxx #用户信息表 ";
Execute SQL statement
$res = $db->query ($sql);
if ($db->iserror ($res))
{
echo "Error executing SQL statement";
}
while ($row = $res->fetchrow (DB_FETCHMODE_ASSOC))
{
$data [] = $row; Put the data in an array
}
Print_r ($data);
You can see that the data has been placed in a multidimensional array.
At this point, our program has been basically completed, and then, we want to define the page to display the data
Open your DW or FrontPage XP, make the displayed page, I made one, and provide to everyone to download
We produced the data display page file as: browesdata.html
/*
This is the data listing interface we're going to display.
<meta http-equiv= "Content-language" content= "ZH-CN" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Data Browsing </title>
<body>
<table border= "1" cellpadding= "0" cellspacing= "0" style= "border-collapse:collapse" bordercolor= "#111111" width= " 100% "id=" AutoNumber1 ">
<tr>
&LT;TD width= "21%" align= "center" bgcolor= "#C0C0C0" > Code </td>
&LT;TD width= "50%" align= "center" bgcolor= "#C0C0C0" > Enterprise name </td>
&LT;TD width= "29%" align= "center" bgcolor= "#C0C0C0" > Telephone </td>
</tr>
<tr>
&LT;TD width= "21%" > </td>
&LT;TD width= "50%" > </td>
&LT;TD width= "29%" > </td>
</tr>
</table>
</body>
 code as follows:

*/
I processed it into an XSLT format HTML document
/*
<?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= "/" >
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Data Browsing </title>
<body>
<table border= "1" cellpadding= "0" cellspacing= "0" style= "border-collapse:collapse" bordercolor= "#111111" width= " 100% "id=" AutoNumber1 ">
<tbody>
<tr>
&LT;TD width= "21%" align= "center" bgcolor= "#C0C0C0" > Code </td>
&LT;TD width= "50%" align= "center" bgcolor= "#C0C0C0" > Enterprise name </td>
&LT;TD width= "29%" align= "center" bgcolor= "#C0C0C0" > Telephone </td>
</tr>
<xsl:for-each select= "Root/result/row" >
<tr>
&LT;TD width= "21%" > <xsl:value-of select= "nsrnm"/></td>
&LT;TD width= "50%" > <xsl:value-of select= "QYMC"/></td>
&LT;TD width= "29%" > <xsl:value-of select= "Qydh"/></td>
</tr>

</xsl:for-each>

</tbody>

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

*/
$htmlFile = "browesdata.html";
$HTMLSTR = readxsl ($htmlFile); To read an HTML document in XSLT format into a variable
Echo gethtml ($data, $HTMLSTR);
End of program
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.