2013-03-19 125 views
2

我有一個PHP文件,將顯示來自Mysql數據庫的一些產品。這工作沒有任何問題。PHP從Mysql數據創建XML文件?

但我需要從這個PHP文件創建一個XML文件,以便能夠加載到閃存。

我已經完成了大部分工作,PHP文件在服務器上創建了一個XML文件並提取數據(僅文本格式數據,即產品名稱,價格,細節,添加日期等) ,我不知道該怎麼做的圖像部分!

這是原來的PHP文件:

<?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(); 
?> 

,這是創建XML文件中的PHP文件:

<?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; ?> 

,你可以看到我已經放在這行代碼:

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

在上面的代碼中,但我不斷收到此錯誤: 解析錯誤:sy ntax錯誤,意外的'。'在第21行和第21行是我上面提到的代碼行!

我將不勝感激您的幫助。

感謝

+0

生成XML文件使用PHP和MySQL - http://www.kvcodes.com/2017/03/generate-xml-file-using-php- mysql/ – Kvvaradha 2017-04-02 01:02:48

回答

0

您沒有正確使用雙引號和單引號的,你可能需要刪除$行[]部分。 你行

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

應該

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

非常感謝您的幫助。現在我得到這個錯誤:XML分析錯誤:文檔元素後的垃圾,同一行上的未定義索引!有什麼建議麼? – 2013-03-19 11:48:44

+0

@DavidSmith我編輯了我的答案 – 2013-03-19 11:57:58