Parsing xml feed problem

Hello anyone. Thanks for the help in advance. Here is the problem. I am taking xml feed with 10 different rows <offer>:


<offers>
<offer>
<id></id>
<url></url>
<amount></amount>
<price>0.</price>
<seller><id></id><name></name></seller>
</offer>
<offer>
<id></id>
<url></url>
<amount></amount>
<price>0.</price>
<seller><id></id><name></name></seller>
</offer>
.
.
.
<offer>
<id></id>
<url></url>
<amount></amount>
<price>0.</price>
<seller><id></id><name></name></seller>
</offer>
<offer>

The php code is:


$xml = simplexml_load_file("URL TO XML FEED");

$seller = $xml->{'offer'}->{'seller'}->{'name'};
$sellerid = $xml->{'offer'}->{'seller'}->{'id'};
$amount = $xml->{'offer'}->{'amount'};
$price = $xml->{'offer'}->{'price'};
$url = $xml->{'offer'}->{'url'};
$flag = '/images/flags/'.$row['desig'].'.png';
$flag = strtolower($flag);

$sql = ("INSERT INTO market (id, country, flag, industry, quality, seller, sellerid, amount, price, link)
		VALUES ('$id', '$name1', '$flag', '$name2', '$name3', '$seller', '$sellerid', '$amount', '$price', '$url')") or  die('<b>Data Insert Error:</b> ' . mysql_error());

  if (!mysql_query($sql,$con1))
  {
  die('Error: ' . mysql_error());
  }

This result is so only the first offer is inserted into the MySQL database. How can I make so all 10 offers to be inserted?

Any help much appreciated.

Directly accessing $xml->{‘offer’} gives first node only, not all nodes. So you have to loop it.


$xml = simplexml_load_file("URL TO XML FEED");

foreach($xml->{'offer'} as $offer)
{
$seller = $offer->{'seller'}->{'name'};
$sellerid = $offer->{'seller'}->{'id'};
$amount = $offer->{'amount'};
$price = $offer->{'price'};
$url = $offer->{'url'};
$flag = '/images/flags/'.$row['desig'].'.png';
$flag = strtolower($flag);

$sql = ("INSERT INTO market (id, country, flag, industry, quality, seller, sellerid, amount, price, link)
		VALUES ('$id', '$name1', '$flag', '$name2', '$name3', '$seller', '$sellerid', '$amount', '$price', '$url')") or  die('<b>Data Insert Error:</b> ' . mysql_error());

  if (!mysql_query($sql,$con1))
  {
  die('Error: ' . mysql_error());
  }
}

Thanks. All OK now. :slight_smile: