如何使用XML在PHP中显示图像?

如何使用XML在PHP中显示图像?,php,xml,simplexml,Php,Xml,Simplexml,我想做一辆购物车。代码工作正常,直到图像部分 <?php $productID = $_GET['product_id']; $xml = simplexml_load_file("product.xml"); $searchedproduct = $xml->xpath('/products/product[product_id="'.$productID.'"]'); $image= $xml->product->image_path; foreach($sear

我想做一辆购物车。代码工作正常,直到图像部分

<?php
$productID = $_GET['product_id'];
$xml = simplexml_load_file("product.xml");

$searchedproduct = $xml->xpath('/products/product[product_id="'.$productID.'"]');
$image= $xml->product->image_path;

foreach($searchedproduct as $productinfo){
foreach ($productinfo as $productdetail){

echo $productdetail->getName(). ": " ;
echo $productdetail . "<br/>";
}

所以我设法找到了答案。。if函数需要插入到foreach函数中,我以前没有把它放进去

?php
$productID = $_GET['product_id'];
$xml = simplexml_load_file("product.xml");

$searchedproduct = $xml->xpath('/products/product[product_id="'.$productID.'"]');

foreach($searchedproduct as $productinfo){
foreach ($productinfo as $productdetail){
if($productdetail->getName($image) == 'image_path'){
echo '<img src="'.$image.'" height="100"; "width="100" ;>';
}
else{
echo $productdetail->getName(). ": " ;
echo $productdetail . "<br/>";
}
}
}
?>

This is my XML file product.xml:

<product>
<category>Clothing</category>
<product_id>0236</product_id>
<title>Devon Denim Jacket</title>
<description>
</description>
<price>39.95 </price>
<image_path>product/nad/images/devon.jpg</image_path>
</product>

<product>
<category>Clothing</category>
<product_id>0238</product_id>
<title>Charlie Crew Fleece </title>
<description>
</description>
<price>24.95 </price>
<image_path>product/nad/images/graphic.jpg</image_path>
</product>
</products>
?php
$productID=$\u获取['product\u id'];
$xml=simplexml\u加载文件(“product.xml”);
$searchedproduct=$xml->xpath('/products/product[product_id=“'.$productID.']”);
foreach($searchedproduct作为$productinfo){
foreach($productinfo作为$productdetail){
如果($productdetail->getName($image)='image\u path'){
回声';
}
否则{
echo$productdetail->getName()。“:”;
echo$productdetail。“
”; } } } ?> 这是我的XML文件product.XML: 服装 0236 德文牛仔夹克 39.95 product/nad/images/devon.jpg 服装 0238 查理船员羊毛 24.95 product/nad/images/graphic.jpg
将数据存储在数据库中不是比xml文件更好吗?查看呈现的html输出。然后看图片。路径是否正确?当您查看img标记的源时,属性src中的输出是什么?对于每个产品,img标记的源是“product/nad/images/devon.jpg”。。即使我查看其他产品,img标签仍然是一样的。也许查看图像的编码是错误的?这里有很多问题。但最重要的是只需查看生成的HTML并验证图像路径设置是否正确。步骤2:找出浏览器要查找的路径。您的代码可能很好,但您的目录和完全相关的URL可能会被破坏。第3点:您确实意识到您需要在Web服务器可以读取的驱动器上放置一个相应的映像文件,对吗?
<product>
<category>Clothing</category>
<product_id>0236</product_id>
<title>Devon Denim Jacket</title>
<description>
</description>
<price>39.95 </price>
<image_path>product/nad/images/devon.jpg</image_path>
</product>

<product>
<category>Clothing</category>
<product_id>0238</product_id>
<title>Charlie Crew Fleece </title>
<description>
</description>
<price>24.95 </price>
<image_path>product/nad/images/graphic.jpg</image_path>
</product>
</products>
?php
$productID = $_GET['product_id'];
$xml = simplexml_load_file("product.xml");

$searchedproduct = $xml->xpath('/products/product[product_id="'.$productID.'"]');

foreach($searchedproduct as $productinfo){
foreach ($productinfo as $productdetail){
if($productdetail->getName($image) == 'image_path'){
echo '<img src="'.$image.'" height="100"; "width="100" ;>';
}
else{
echo $productdetail->getName(). ": " ;
echo $productdetail . "<br/>";
}
}
}
?>

This is my XML file product.xml:

<product>
<category>Clothing</category>
<product_id>0236</product_id>
<title>Devon Denim Jacket</title>
<description>
</description>
<price>39.95 </price>
<image_path>product/nad/images/devon.jpg</image_path>
</product>

<product>
<category>Clothing</category>
<product_id>0238</product_id>
<title>Charlie Crew Fleece </title>
<description>
</description>
<price>24.95 </price>
<image_path>product/nad/images/graphic.jpg</image_path>
</product>
</products>