PHP combined with XML, XSLT, and MySQL implementation code _php tutorial

Source: Internet
Author: User
Tags dsn pear xsl xsl file xslt
Using XML and XSLT in PHP to use some DLL libraries,
Extension=php_domxml.dll//function library for manipulating XML
Extension=php_iconv.dll//transcoding, such as the transfer of GB2312 to UTF-8
Extension=php_xslt.dll//xslt's Library of functions
With these three libraries, you will add the DLLs directory in the PHP installation directory to the path, or you will not find
Of these class libraries
In the following course, we will use the PHP Pear Class Library, the main use of DB Class Library, Sql2xml class library, you can
Pear.php.net Download the latest version
After downloading the above two class library, 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 Apache boot is not normal to use

This is done using the XML and XSLT settings in PHP, which is not very simple:)
Users who use Linux can go to
http://www.gingerall.com/Download the source code about XSLT
http://www.gnu.org/software/libiconv/Download the source code about ICONV
Two PHP and XML, XSLT, MySQL, the use of a combination of primary articles
What I'm going to talk about is a simple process of extracting data from a database, generating an XML document, and using XSLT to transform into HTML.
This process only speaks of the process of using this technology and does not involve paging and other technologies
The example I use the following database tables and data
Table Name: Enterprise User Information Form, English name: yhxx
Table structure:
#
# The structure of the data table ' 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 Form ';
#
# Data Sheet content ' yhxx '
#
INSERT into Yhxx VALUES (' 310109040111985 ', ' Joe Home Diet Development Limited ', ' 8621-63346626 ');
INSERT into Yhxx VALUES (' 310104040221736 ', ' Shanghai Jialing Trading Co., Ltd. ', ' 74292546 ');
INSERT into Yhxx VALUES (' 310108040331576 ', ' Emerald Art Company ', ' 54861465 ');

Next I started writing the data Extraction program and the page that displayed the data.
In order for everyone to understand, I use the simplest way to write a program
program file name: browesdata.php
Page file name: browesdata.html
Program and page file to provide download, this program has been tested under Win2000, MySQL
Code See next page

If you are interested in this technology, let me tell you a little bit more about PHP and XML, XSLT, and database in a deeper step.
Copy CodeThe code is as follows:
Require_once "db.php"; Database processing classes in the 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 for Connected databases
$db = Db::connect ($DSN); Connecting to a database
if (Db::iserror ($DB))
{
Die ($db->getmessage ()); Connection failure, output error message
}
The following two is a public function
/**
* Read XSL Document
*
* @param String $filename The name of the-xsl file
* @return String
*/
function readxsl ($filename)
{
if (false==file_exists ($filename))
{
echo "The file to be read $filename does not exist
";
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-arrays variable
* @param String $xslstring-xsl document data
* @param String $xmlstring-xml document data
*/
function gethtml ($arydata = False, $xslstring = False, $xmlstring = False)
{
Global $db; Use the $db object that you just
Include_once ("xml/sql2xml.php"); To include Sql2xml in
$sql 2xmlclass = new Xml_sql2xml ($DB); Instantiate a Sql2xml
$sql 2xmlclass->setencoding ("GB2312"); Set the transcoding type of the 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 the transformation of 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 will define the page to display the data
Open your DW or FrontPage XP, make the displayed page, I made one and offer it to everyone to download
The data we produced shows the paging file as: browesdata.html
/*
This is the data list interface that we normally want to display.




<title>Data browsing</title>













Code Company Name Phone
     




Copy CodeThe code is as follows:
*/
I processed it into an XSLT-formatted HTML document
/*







Data browsing




















Code Company Name Phone
     






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

http://www.bkjia.com/PHPjc/320842.html www.bkjia.com true http://www.bkjia.com/PHPjc/320842.html techarticle using XML and XSLT in PHP to use some DLL libraries, Extension=php_domxml.dll//Manipulate XML Library Extension=php_iconv.dll//transcoding, For example, turn GB2312 into UTF-8 extensio ...

  • 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.