How to display image in PHP with XML?

て烟熏妆下的殇ゞ 提交于 2019-12-23 05:09:22

问题


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!"; }
}
?>

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>

回答1:


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>


来源:https://stackoverflow.com/questions/22572299/how-to-display-image-in-php-with-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!