XML and PHP Simplified - XML,PHP and the Database / Page 2 | WebReference

XML and PHP Simplified - XML,PHP and the Database / Page 2


[prev]

XML and PHP Simplified - XML,PHP and the Database [con't]

The Code Explained

Now we look at the code that made up the program that we wrote in the previous section. It is important for us to know how to use database information in an XML document, so I think that we need to have a look at the code before continue to discuss the DOM. The first script is called inputform.php, this script is responsible for presenting the user with an HTML form that will enable him or her to input XML document information. This information is then added to the database. Below is the code:

The HTML code for the script is as follows:

The code for the inputform.php script is relatively easy to understand. The HTML code collects three pieces of information, the name of the contact, the email address and finally the address of the contact. You will note that the form fields match exactly the database table fields as well as the tags in our XML document. So lets take a look at the PHP code section:

First we check if the form has been submitted:

If the form has been submitted then we start by setting the variables that we want to use:

Then we start the data validation process. We basically require all the fields to be filled in and second, we know that all fields should be of type string. So, we start by checking to see if the form fields are filled:

If any of the fields have been left empty then we would set the $msg variable, otherwise it would be empty. We want to continue inserting the data into the database so we need to be sure that the data validation process went without a glitch. So we test the $msg variable before continuing:

If the $msg variable is empty we continue inserting the data into the database. We create an SQL statement to do the actually data insertion:

Then we run the SQL statement:

$r = mysql_query($q);

Now we test to see if the outcome of the insertion process has been successful; if not we add an error message:

The write2xmldoc.php script does exactly what the name suggests; it writes database data to an xml document. It basically overwrites the contents of the existing XML document with the same name, with the data that it extracts from the database. Below is the code for the script:

The HTML code is as follows:

The code for this script is a little bit more involved compared to the previous script. So lets look at it closely. First, we set the name of the xml document that we want to use in this exercise:

Then we check if a file with the same name already exists, if not we continue:

We open the file and overwrite any contents that it may have. The "w+" option ensures that the contents or file is overwritten:

Now we start to write to the file; remember that the data that we are going to write should come from the database since we want this xml document to contain the latest contact details. We start by setting the XML document version; we are actually creating a new document, so we have to start from the very beginning:

Then we set the root element and child element:

Now we are ready to connect to the database and retrieve the information that we require. First, we set connection details:

Then we try to connect to the database server using the mysql_connect() function and then we select the database that we want to use:

Once the database is selected, we create the SQL statement that is going to extract all the records that is contained in the database:

$q = "SELECT * FROM contact";

We then run the query:

$r = mysql_query($q);

If the query was successful then the $r variable in which the results of the query is stored will contain something. We test accordingly:

if($r){

Then we run a loop to extract the stored records from the array:

while($row = mysql_fetch_assoc($r)){

With each iteration we write the name, email and address information about the contact. We use the tags as we've describe them previously to properly format them:

When we finish writing the details of the contacts, we close the main element of the document:

Then we close the file:

If the query did not return any records then we set an error message:

If the file has been successfully written, we inform the user of it and point the viewer to a location where the file has been stored. The user can then click on this link to view the updated xml document:

Otherwise, we set the error messages if the file could not be created:

In the next article, we will be discussing how to present XML document data in a neat and clear way using its CSS equivalent.

Original: July 15, 2009


[prev]