Tutorials to .com

Tutorials to .com » Php » Xml » Second php with XML, XSLT, Mysql combination of the use of code chapter

Second php with XML, XSLT, Mysql combination of the use of code chapter

Print View , by: iSee ,Total views: 7 ,Word Count: 1359 ,Date: Thu, 16 Apr 2009 Time: 1:56 AM

<? php
require_once "DB.php"; / / PEAR database class
$ 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"; / / connect the DNS database preparation
$ db = DB:: connect ($ dsn); / / connect to database
if (DB:: isError ($ db))
(
die ($ db-> getMessage ()); / / Connection failed, output an error message
)

/ / The following function of two public
/ **
* Read the xsl file
*
* @ Param String $ filename - xsl document name
* @ Return string
* /
function readXsl ($ filename)
(
if (false == file_exists ($ filename))
(
echo "the file to read <font color='red'> $ filename </ font> does not exist </ br />";
return false;
)
return implode ('', file ($ filename));
) / / End function readXsl

/ **
* The xml document or an array of variables in accordance with xsl file into HTML content
*
* @ Param array $ arydata - an array of variables
* @ Param String $ xslstring - xsl document data
* @ Param String $ xmlstring - xml document data
* /
function getHtml ($ arydata = false, $ xslstring = false, $ xmlstring = false)
(
global $ db; / / using the $ db object just
include_once ( "XML/sql2xml.php"); / / to contain sql2xml in.
$ sql2xmlclass = new xml_sql2xml ($ db); / / will be instantiated sql2xml
$ sql2xmlclass-> setEncoding ( "GB2312"); / / set the data type of transcoding
if (false == $ xmlstring) (/ / If the user data into an array, then use the array data to xsl
/ / Set data to generate xml document node name
$ options = array (tagNameRow => "row",
tagNameResult => "result"
);
$ sql2xmlclass-> SetOptions ($ options);
/ / Add XML documents to generate data
$ sql2xmlclass-> add ($ arydata);
)
/ / Get xml document
$ xmlstring = $ sql2xmlclass-> getxml ();
/ / print $ xmlstring;
/ / Start the following XML data document into HTML using XSLT document
$ 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 "convert xml data to xsl Error"; [AutoPage]
xslt_free ($ xh);
)
) / / End function getHtml ()




/ / From the user information table of the SQL statement to query the data
$ sql = "select
nsrnm, # code
qymc, # Name
qydh # telephone
from
yhxx # user information table ";
/ / Implementation of SQL statements
$ res = $ db-> query ($ sql);
if ($ db-> isError ($ res))
(
echo "Error in the implementation of SQL statement";
)
while ($ row = $ res-> fetchRow (DB_FETCHMODE_ASSOC))
(
$ data [] = $ row; / / data into an array of
)
/ / print_r ($ data);
/ / We can see the data has been placed in a multi-dimensional array in the
/ / At this point, our program has been basically completed, and then the next, we have to define the page display data
/ / Open your DW or frontpage XP, the production of display pages, I had a, and made available to you to download

/ / We create the data file for the page: browesData.html
/ *
This is what we usually want to display a list of interface data
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
Browse <title> data </ 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"> Name </ td>
<td width="29%" align="center" bgcolor="#C0C0C0"> phone </ td>
</ tr>
<tr>
<td width="21%"> </ td>
<td width="50%"> </ td>
<td width="29%"> </ td>
</ tr>
</ table>
</ body>
</ html>


* /

/ / I put it into an XSLT processing of the HTML document 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" />
Browse <title> data </ title>
</ head>
<body>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1"> [AutoPage]
<tbody>
<tr>
<td width="21%" align="center" bgcolor="#C0C0C0"> code </ td>
<td width="50%" align="center" bgcolor="#C0C0C0"> Name </ td>
<td width="29%" align="center" bgcolor="#C0C0C0"> 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); / / the xslt file to read HTML format to the variable
echo getHtml ($ data, $ htmlStr);

/ / Procedure

?>


Php XML Application Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.