PHP with XML, XSLT, MySQL, the combination of application, code text

Source: Internet
Author: User
Tags array dsn xmlns xsl xsl file xslt mysql database
Mysql|xml

<?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>


*/

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= "/" >
http://www.w3.org/1999/xhtml">
<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

?>



Related Article

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.