Foreach loop problem

I want to extract data from an xml file and insert into variables. My code is working but is only showing the first records rather than all the records. What am I doing wrong?

My code is:

<?php

$mpf = simplexml_load_file ("feed/retro.xml");

foreach ($mpf->merchant as $merchant) {
	$merchant_name = $merchant['name'];
	$prodID = $merchant->prod['id'];
	$in_stock = $merchant->prod['in_stock'];
	$prodName = $merchant->prod->text->name;
	$price = $merchant->prod->price->buynow;
	
	echo $merchant_name.'<br>';
	echo $prodID.'<br>';
	echo $prodName.'<br>';
	echo '£'.$price.'<br>';
	echo 'In stock: '.$in_stock.'<br>';
	echo '**************<br>';
}

?>

Thanks

Can you post a sample of the XML data?

Here is an exerpt from my xml file


<merchantProductFeed>
<merchant id="8" name="Drinkstuff.com">
<prod id="57801251" in_stock="yes">
<pId>1264-553</pId>
<text>
<name>Lord of the Rings Pinball</name>
<desc>The greatest adventure of all time is now Stern Pinball&amp;#8217;s epic pinball machine for all ages. THE LORD OF THE RINGS&amp;#8482; pinball machine features exciting elements from New Line Cinema&amp;#8217;s box office hits, THE FELLOWSHIP OF THE RING&amp;#8482; and THE TWO TOWERS, as well as the most anticipated installment yet, THE RETURN OF THE KING&amp;#8482;</desc>
</text>
<uri>
<awTrack>http://www.awin1.com/pclick.php?p=57801251&amp;a=XXXXX&amp;m=8</awTrack>
<awThumb>http://images.productserve.com/thumb/8/57801251.jpg</awThumb>
<awImage>http://images.productserve.com/preview/8/57801251.jpg</awImage>
<mLink>http://www.drinkstuff.com/products/affiliate.asp?affID=987654321&amp;prodID=553</mLink><mImage>http://www.drinkstuff.com/productimg/14184.jpg</mImage>
</uri>
<price><buynow>3993.18</buynow>
<delivery>0.00</delivery>
</price>
<cat>
<awCatId>577</awCatId>
<awCat>Retro Games</awCat>
<mCat>Arcade Machines</mCat>
</cat>
</prod>
</merchant>
</merchantProductFeed>

This is just one record from a particular merchant.

Thanks


<?php

$mpf = simplexml_load_file ("feed/retro.xml");

foreach ($mpf->children() as $merchant) {
	$merchant_name = $merchant['name'];
	$prodID = $merchant->prod['id'];
	$in_stock = $merchant->prod['in_stock'];
	$prodName = $merchant->prod->text->name;
	$price = $merchant->prod->price->buynow;
	
	echo $merchant_name.'<br>';
	echo $prodID.'<br>';
	echo $prodName.'<br>';
	echo '&#163;'.$price.'<br>';
	echo 'In stock: '.$in_stock.'<br>';
	echo '**************<br>';
}

?>

Cheers for that.

Ok, just tried that adjustment to my code but it still only displays the first record for each merchant and not all products from each merchant. Any ideas would be greatly appreciated.

Well, you’re iterating one merchant at a time then displaying the first product item information, or so it appears…

Try this Richard. :wink:


<?php
foreach($xml->merchant as $merchant){
    echo '<h1>', $merchant['name'], '</h1>';
    foreach($merchant->prod as $product){
        echo '<p>Item: ', (string)$product->name, '</p>';
    }
}
?>

Thanks for the rapid response. Tried your code it works great. Had to make a minor adjustment to display the product name. Many thanks for the help. Have to say that this forum has helped me out no end and it’s great to know that help is at hand. I have learnt so much.

I have another question however, if I want to insert the values returned into my database can I use the same structure to assign variables rather than echoing out?

Sure. :slight_smile:

Sitepoint does indeed rock.

Because I’m using foreach loops to get the values, how can I construct the INSERT query? If I make the query outside the loop won’t the variable be populated with the last record? Or will I have to construct the query in parts? No need to reply sorted it myself. Thanks anyway. Wahey!!! Over 100 posts!