PHP, XML, XSLT, and MySQL code

Source: Internet
Author: User
Tags mysql code 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.

now you have to use the XML and XSLT settings in PHP, is it very simple?
Users who use Linux can download the tddl source code at
http://www.gingerall.com/< br> sources
the combination of PHP and XML, XSLT, and MySQL,
The following is 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:
# structure of the data table '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 ';
#< br> # data table content 'hxx'
#< br> insert into yhxx values ('123 ', 'qiao Jia Food Development Co., Ltd. ', '2017-8621');
insert into yhxx values ('20170101', 'shanghai Jialing Trading Co., Ltd. ', '20170301 ');
insert into yhxx values ('20160301', 'emerald art company', '20160301');

Next, I will write the Data ExtractionProgramAnd display data 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.
CodeSee 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 code The 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 code The 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.