Question

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>
Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top