Hi I need to use PHP to get data from an XML document of items.
The XML doc looks like this
<STOREITEMS>
<PRODUCT ITEM="dj91ja" NAME="Mushroom Soup">
<STOCK>In Stock</STOCK>
</PRODUCT>
<PRODUCT ITEM="09shjk1" NAME="Chicken Soup">
<STOCK>In Stock</STOCK>
</PRODUCT>
<PRODUCT ITEM="mgm826ga" NAME="Vegetable Soup">
<STOCK>No Stock.</STOCK>
</PRODUCT>
</STOREITEMS>
I need to find out if a product is in stock based on the ITEM attribute, for example 09shjk1
This is my code:
$product_queried = '09shjk1';
$xml=simplexml_load_string($xmlstr) or die("Error: Cannot make object");
foreach($xml->children() as $items) {
if($items->PRODUCT['ITEM'] == $product_queried){
echo 'Product stock : ' . $items->STOCK . '';
}
}
Unfortunately it's not working. I need to be able to change the $product_queried to get the stock of the item from the XML document.
Can anyone advise? Sorry if it's completely wrong I am new to XML. Many thanks in advance 🙂