0

Trying to pull an id from an xml file, pass it into an api query, and load the results into a dom document. Thing is my foreach loop is only returning the first iteration, then seems to stop.

Why isn't it going back to fetch the next PROGRAM_ID?

//load results of first api call into simplexml - print_r here gives me a big array with all the expected rows in it
$progsitecontent = simplexml_load_file($progsiteapi);

//set which nodes to step through to reach required information
$totalprogsitecontent = $progsitecontent->matrix->rows->row;

//for each instance of a program id in this simplexml file:
foreach($totalprogsitecontent->PROGRAM_ID as $progid)
{

    //...substitute the program id into the api call
    $programdetails = $progdetailsapi_start.$progid.$progdetailsapi_end;
    $complete_program_details = simplexml_load_file($programdetails);

    //now for each instance of a programs info, load into a DOM document and carry out the below actions - from here down already works in another script so im sure the problem has to be above this point 

    $prog_info = $complete_program_details->matrix->rows->row;

    //create the top line container tag
    $row = $doc->createElement ("programInformation");

    //create the container tag
    $progID = $doc->createElement("programId");
    //fill it with the information you want
    $progID->appendChild ( $doc->createTextNode ( $prog_info->PROGRAM_ID ) );
    //attach this information to the row
    $row->appendChild($progID);

    //repeat for each element you want to include
    $progName = $doc->createElement("programName");
    $progName->appendChild ( $doc->createTextNode ( $prog_info->PROGRAM_NAME ) );
    $row->appendChild($progName);

    $progURLs = $doc->createElement("programUrls");
    $progURLs->appendChild ( $doc->createTextNode ( $prog_info->PROGRAM_URLS ) );
    $row->appendChild($progURLs);

    $progLogo = $doc->createElement("programLogo");
    $progLogo->appendChild ( $doc->createTextNode ( $prog_info->MERCHANT_LOGO ) );
    $row->appendChild($progLogo);

    $r->appendChild ($row);

}

echo $doc->saveXML();

Feel free to comment on how any of this has been written. I'm still at the stage of "bodge-it-and-see" :)

7
  • What is the result of $totalprogsitecontent? Commented Sep 5, 2012 at 10:56
  • post the xml part for $totalprogsitecontent Commented Sep 5, 2012 at 11:03
  • is $totalprogsitecontent->PROGRAM_ID an array? Commented Sep 5, 2012 at 11:07
  • I've changed this slightly, so $totalprogsitecontent has become $progsitecontent. A print_r of $progsitecontent gives me a big array, not xml. Commented Sep 5, 2012 at 11:13
  • didnt mean to press return there......back in a few mins with the xml Commented Sep 5, 2012 at 11:14

1 Answer 1

1

Can't say much without seeing the full result of $totalprogsitecontent, but I think it should look something like this:

foreach($totalprogsitecontent as $progid)
{
...
}

Since $totalprogsitecontent->PROGRAM_ID is already a single value - so you're iterating over this element instead of the array.

Also, your $progid is lowercase in the for loop but you refer to $progID -- PHP is case sensitive.


After looking at your XML code here's what it should look like.

foreach($progsitecontent->matrix->rows->row as $row){
     $progid = $row['PROGRAM_ID'];
     $affid = $row['AFFILIATE_ID'];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the notes. I've changed the variables to be the same case, but removing the ->matrix->rows->row->PROGRAM_ID has stopped it from returning even the first iteration
Hi Nick, that sorted it, thanks! The problem was that I was drilling down one node too far in the xml. I was going for each piece of data under matrix->rows->row->programID What I actually wanted was to go for each piece of data under matrix->rows->row then grab the detail from the $row object Cheers

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.