0
votes

I am trying to call an image in my PHP to an XML file. I have tried to do this many different ways, but have not had much luck. More specifically, I am trying to call img file variable "Image1" to display. I have also sought tutoring, but the tutor was also somehow stumped on this, so any help is appreciated.

Prompt: (a) Create a XML file which contains the description of at least 3 product names, prices, and image file names.

(b) Load the XML file using Php and generate the shopping items: image, name, price and allow order quantity. Hint: Replace the image file names, item names, and prices with the Php codes. The Php codes supply the Php variables which were filled by reading the XML file at the beginning of the page. Then use "echo" verb to show the content of the session variable which contains the file name, item name, and price. The page should have the file extension .php.

//My XML
<?xml version="1.0" encoding="UTF-8"?>

<items>
<object>
<Name>CSU Womens T-Shirt</Name>
<Material>3% Polyester / 97% Cotton</Material>
<Price>$23.99</Price>
<image1>C:\Users\erica\OneDrive\Documents\IST450HW\IMGs\csupic.jpg</image1>
</object>
<object>
<Name>CSU Unisex Hat</Name>
<Material>2% Spandex / 98% Cotton</Material>
<Price>$10.99</Price>
</object>
<object>
<Name>CSU Men's T-Shirt</Name>
<Material>5% Polyester / 95% Cotton</Material>
<Price>$19.99</Price>
</object>
</items>

//My PHP
<?php
$xml = simplexml_load_file("Items.xml");

foreach ($xml as $key => $value) {
  foreach ($value as $key => $value) {
    echo $key.": ".$value."</br>";
  }
}
 $result = $xml->$items->$object->$image1;
echo '<img src="'.$result.'" height="100"; "width="100" ;>';
?>
2
Your URL is a local file system path? Is that your plan? - Scuzzy
Do you see anything wrong with foreach ($value as $key => $value)? - Tim Morton
@Scuzzy Yes, I have the image is stored on my local C drive. "C:\Users\erica\OneDrive\Documents\IST450HW\IMGs\csupic.jpg" - Erica Morris
@TimMorton Honestly, I just started learning PHP about 2 days ago. I researched the function for calling values and I wasn't sure if the stated "value" and "key" worked as variables or methods. - Erica Morris
Ah. The problem that I saw is that you are overwriting $value, so your outer loop is immediately trashed. I don't think you need $key in the foreach, because you're iterating through the values (properties). There are no methods in a simplexml object. Take a look at stackoverflow.com/questions/4637617/… for how to iterate through the xml. - Tim Morton

2 Answers

0
votes

$xml will be an object, im not sure why you are looping through it with a foreach, so ill ignore that bit.

To access the element which you're after you need to do it like:

$result = $xml->object[0]->image1;
echo '<img src="'.$result.'" height="100" width="100"/>';

But your browser won't see that path (or it wont work once you put it elsewhere), so place the image in an accessible location e.g: ./imgs and then change your xml.

For example:

<image1>./imgs/csupic.jpg</image1>

Then the resulting img tag will look like:

<img src="./imgs/csupic.jpg" height="100" width="100"/>

The web server will then be able to serve the file without issues.

-2
votes

Try put image file csupic.jpg in the same dir of php file and if not work change echo line to:

echo '<img src="/'.$result.'" height="100"; "width="100" ;>';