2

I'm trying to get a product list for a client from a webservices xml file over to an SQL databse using a little php script, but I can't seem to get it to work.

The relevant code is as follows:

$c = 0;  
...
$xml = simplexml_load_file($completeurl);
$listingsArray = $xml->listings->listing;

foreach($listingsArray as $listing){
    $addition[0] = $listing[$c]->type;
    $addition[1] = $listing[$c]->condition;
    //etcetera
    c = c + 1;
}

The XML file is formated like:

<inventory>
    <listings>
        <listing>
            //tags for type, condition, etc
        </listing>
    </listings>
</inventory>

$completeurl is a string that contains the url of the xml file $addition is an array that's defined earlier in the code

I've been working on this for a while now, but I can't seem to figure out where the error is in my code. The problem that I'm having is that $listingsArray should have close to 100 elements in it, but is constantly coming up with 0. Anybody see what I'm doing wrong?

EDIT: I tried changing

$listingsArray = $xml->listings->listing;

to

$listingsArray = $xml->listings;

But empty strings are still being written to the $addition array. A var_dump of listingsArray show that all of the information is in there, though.

1
  • @cOle2 $c is set to 0 before the foreach loop and iterates with it. Forgot to copy it in. Commented Apr 11, 2012 at 17:59

2 Answers 2

1

If you want each listing i believe you are going a child too deep.

$listingsArray = $xml->listings->listing;

shoudl be

$listingsArray = $xml->listings;

foreach($listingsArray as $listing){
    $addition[0] = $listing[$c]->type;
    $addition[1] = $listing[$c]->condition;
    //etcetera
}

additionally what is $c? It also helps to post your exact errors. When debugging inserting var_dumps is extremely helpful. If you think the probelm is $listingArray print it out and see if it contains the data you want.

Sign up to request clarification or add additional context in comments.

1 Comment

$c is a counter that iterates with the foreach loop, so it goes through each of the <listing>'s. I did a var dump and all of the data I need seems to be there, but it's still not writing into the array. I was, however, going one child too deep.
0

changing

$listingsArray = $xml->listings->listing;

to

$listingsArray = $xml->listings;

Should solve the issue. You set $listingsArray to the array of listings, then the foreach tries to go down another level

2 Comments

I changed it to that, but it's still not working (though that was a mistake). A var_dump of $listingsArray shows that it's picked up over 100 listings, but attempting to put the listing data into the $addition array still isn't working
Every time you go through the foreach(), $addition[0] (and [1]) are being overwritten. Should that be $addition[$c][0] instead? The latter seems to make more sense with your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.