Php, XML, XSLT, and Mysql code

Source: Internet
Author: User
Tags mysql code xsl xsl file xslt

Some dll libraries are required to use xml and xslt in php,
Extension = php_domxml.dll // function library for operating XML
Extension = php_iconv.dll // For transcoding, such as converting GB2312 to UTF-8
Extension = php_xslt.dll // function library of XSLT
When using the above three libraries, you need to add the dlls directory under the php installation directory to the path, otherwise it will not be found
The
In the following course, we will use the PEAR class library in php, mainly using the DB class library and Sql2XML class library.
Download the latest pear.php.net version.
After downloading the above two class libraries, you 'd better set them in php. ini.
Include_path = ".; d: phppear"
D: phppear is the installation path of my pear.
After the configuration is complete, restart the machine. Otherwise, Apache cannot be started properly because the path cannot be found.

So far, we have to use the XML and XSLT settings in PHP. Is it very simple :)
Linux users can
Http://www.gingerall.com/download the source code for release t.
Http://www.gnu.org/software/libiconv/download source code for iconv
The combination of php, XML, XSLT, and Mysql
Next I will talk about a simple process of extracting data from the database, generating XML documents, and converting to HTML using XSLT,
This process only describes the process of using this technology, and does not involve paging and other technologies.
In this example, I use the following database tables and data
Table Name: enterprise user information table, English name: yhxx
Table Structure:
#
# Data table structure 'hxx'
#
Create table yhxx (
Nsnm varchar (15) not null default '',
Qymc varchar (200) not null default '',
Qydh varchar (50) not null default '',
Primary key (nsnm)
) TYPE = MyISAM COMMENT = 'user info table ';
#
# Data table content 'hxx'
#
Insert into yhxx VALUES ('20170101', 'qiao Jia Food Development Co., Ltd. ', '2017-123 ');
Insert into yhxx VALUES ('20170101', 'shanghai Jialing Trading Co., Ltd. ', '20170101 ');
Insert into yhxx VALUES ('20170101', 'emerald art company', '20160301 ');

Next, I will write the data extraction program and the data display page.
To make everyone understand, I use the simplest method to write programs.
Program file name: browesData. php
Page file name: browesData.html
Download programs and page files. This program has passed testing in win2000 and mysql.
For the code, see the next page.

If you are interested in this technology, I will share with you some more in-depth application of php in combination with xml, xslt, and databases.
Copy codeThe Code is as follows:
<? Php
Require_once "DB. php"; // database processing class in PEAR
$ DataType = "mysql"; // Database Type
$ User = "root"; // user Name
$ Pass = "abcd"; // Password
$ Host = "202.96.215.200"; // address of the Mysql database server
$ Db_name = "test"; // Database Name
$ Dsn = "$ dataType: // $ user: $ pass @ $ host/$ db_name"; // configure the DNS for connecting to the database
$ Db = DB: connect ($ dsn); // connect to the database
If (DB: isError ($ db ))
{
Die ($ db-> getMessage (); // connection failed, error message output
}
// The following two are public functions.
/**
* Reading the xsl document
*
* @ Param String $ filename-xsl file name
* @ Return string
*/
Function readXsl ($ filename)
{
If (false = file_exists ($ filename ))
{
Echo "the file to be read <font color = 'red'> $ filename </font> does not exist </br/> ";
Return false;
}
Return implode ('', file ($ filename ));
} // End function readXsl
/**
* Convert xml files or array variables into HTML content based on the xsl File
* Http://knowsky.com
* @ Param array $ arydata-array variable
* @ Param String $ response String-xsl document data
* @ Param String $ xmlstring-xml document data
*/
Function getHtml ($ arydata = false, $ response string = 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 data transcoding type.
If (false = $ xmlstring) {// if the user passes in the array data, apply the array data to the xsl
// Set the node name for generating XML document data
$ Options = array (tagNameRow => "row ",
TagNameResult => "result"
);
$ Sql2xmlclass-> SetOptions ($ options );
// Add the data to generate the XML document
$ Sql2xmlclass-> add ($ arydata );
}
// Obtain the xml document
$ Xmlstring = $ sql2xmlclass-> getxml ();
// Print $ xmlstring;
// Start to convert the XML data document to the HTML document using XSLT below
$ Arguments = array ('/_ xml' => $ xmlstring,
'/_ Xsl' => $ response string
);
$ Xh = effect_create ();
$ Result = effect_process ($ xh, 'arg:/_ xml', 'arg:/_ xsl ', null, $ arguments );
If ($ result ){
Return $ result;
Effect_free ($ xh );
} Else {
Return "An error occurred while converting xml data to xsl ";
Effect_free ($ xh );
}
} // End function getHtml ()


// SQL statement used to query data from the user information table
$ SQL = "select
Nsnm, # code
Qymc, # company name
Qydh # phone number
From
Yhxx # user information table ";
// Execute the SQL statement
$ Res = $ db-> query ($ SQL );
If ($ db-> isError ($ res ))
{
Echo "An error occurred while executing SQL statements ";
}
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 multi-dimensional array.
// At this point, our program has basically been completed. Next, we need to define the page for displaying data.
// Open your DW or FrontPage XP and create the displayed page. I made one and provided it for you to download.
// The data display page file we created is browesData.html.
/*
This is the data list interface we usually want to 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"> company name </td>
<Td width = "29%" align = "center" bgcolor = "# C0C0C0"> telephone </td>
</Tr>
<Tr>
<Td width = "21%"> </td>
<Td width = "50%"> </td>
<Td width = "29%"> </td>
</Tr>
</Table>
</Body>
</Html>

Copy codeThe Code is as follows:
*/
// I process 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"> company name </td>
<Td width = "29%" align = "center" bgcolor = "# C0C0C0"> telephone </td>
</Tr>
<Xsl: for-each select = "root/result/row">
<Tr>
<Td width = "21%"> <xsl: value-of select = "nsnm"/> </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); // read HTML documents in xslt format to variables.
Echo getHtml ($ data, $ htmlStr );
// End the 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.