如何使用 XML 在 PHP 中显示图像? [英] How to display image in PHP with XML?
本文介绍了如何使用 XML 在 PHP 中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作购物车.代码运行正常,直到图像部分.
I’m trying to make a shopping cart. The code works fine until the image part.
<?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/>";
}
此代码有效,但不显示图像...只是一个空白框...这里需要帮助...我应该为每个图像使用数组吗?
This code works but it doesn’t show images... just a blank box... need help here... should I use array for each of the images?
if($productdetail->getName($image) == 'image_path'){
echo '<img src="'.$image.'" height="100"; "width="100" ;>';
}
else{ echo "image not found!"; }
}
?>
这是我的 XML 文件 product.xml
:
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>
推荐答案
所以我设法找到了答案.. if 函数需要插入到我之前没有放置的 foreach 函数中..
So i managed to find out the answer .. the if function need to be inserted inside the foreach function which i didnt put it before ..
?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>
这篇关于如何使用 XML 在 PHP 中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文