我有一个 PHP 文件,它将显示 Mysql 数据库中的一些产品。这没有任何问题。
但我需要从这个 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>"];
在上面的代码中,但我不断收到此错误: 解析错误:语法错误,意外的“。”在第21行,第21行就是我上面提到的那行代码!
非常感谢您的帮助。
谢谢
最佳答案
您没有正确使用双引号和单引号,您可能需要删除 $row[] 部分。 你的线路
$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>";
关于PHP 从 Mysql 数据创建 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15498261/