SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Bulgaria
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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>:

    Code:
    <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:

    Code:
    $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.

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Perundurai, Tamilnadu, India
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Directly accessing $xml->{'offer'} gives first node only, not all nodes. So you have to loop it.

    PHP Code:
    $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());
      }


  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Location
    Bulgaria
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. All OK now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •