我正在尝试制作购物车。代码工作正常,直到图像部分。
<?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($productdetail->getName($image) == 'image_path'){
echo '<img src="'.$image.'" height="100"; "width="100" ;>';
}
else{ echo "image not found!"; }
}
?>
这是我的XML文件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>
答案 0 :(得分:0)
所以我设法找到答案.. 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>