Home:ALL Converter>PHP to create XML file from Mysql data?

PHP to create XML file from Mysql data?

Ask Time:2013-03-19T19:29:48         Author:David Smith

Json Formatter

I have a PHP file that will show some products from Mysql database. this works without any issue.

But I need to create an XML file from this PHP file in order to be able to load it into flash.

I have done the most part and the PHP file creates an XML file on the server and pulls the data (only text format data, i.e. product name, price, details, date added etc etc) and it works perfectly fine BUT, i don't know what to do for the images part!!

This is the original PHP file:

<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
        <tr>
          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>
          <td width="83%" valign="top">' . $product_name . '<br />
            $' . $price . '<br />
            <a href="product.php?id=' . $id . '">View Product Details</a></td>
        </tr>
      </table>';
    }
} else {
    $dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>

and this is the PHP file that creates the XML file:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
header("Content-Type: text/xml"); //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xmlBody .= "<XML>";
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "../config/connect_to_mysql.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $image = $row["<a href="../product.php?id=' . $id . '"><img src="../inventory_images/' . $id . '.jpg" alt="' . $product_name . '"/></a>"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
    $xmlBody .= '
<Data> 
    <DataID>' . $id . '</DataID> 
    <DataTitle>' . $product_name . '</DataTitle>
    <DataDate>' . $price . '</DataDate>
    <DataImage>' . $image . '</DataImage>
    <DataDescr>' . $date_added . '</DataDescr>
</Data>';
} // End while loop
mysql_close(); // close the mysql database connection
$xmlBody .= "</XML>";
echo $xmlBody; // output the gallery data as XML file for flash
}
?>
<?php echo $dynamicList; ?>

as you can see I have placed this line of code:

$image = $row["<a href="../product.php?id=' . $id . '"><img src="../inventory_images/' . $id . '.jpg" alt="' . $product_name . '"/></a>"];

in the code above but I am keep getting this error: Parse error: syntax error, unexpected '.' in line 21 and line 21 is the line of code I mentioned above!

I would greatly appreciate your help.

Thanks

Author:David Smith,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15498261/php-to-create-xml-file-from-mysql-data
yy