Jump to content

[SOLVED] SimpleXML Looping


RabPHP

Recommended Posts

Greetings,

 

Here is an issue I am running into.  I have an XML that looks like the following:

 

<Query>

    <Patient>

          <Study>

              <PatientID>1145566</PatientID>

              <StudyCode>x333333</StudyCode>

          </Study>         

          <Study>

              <PatientID>1145566</PatientID>

              <StudyCode>x222222</StudyCode>

          </Study>

      </Patient>

    <Patient>

          <Study>

              <PatientID>1145566</PatientID>

              <StudyCode>x4444444</StudyCode>

          </Study>

          <Study>

              <PatientID>1145566</PatientID>

              <StudyCode>x6666666</StudyCode>

          </Study>   

          <Study>

              <PatientID>1145566</PatientID>

              <StudyCode>x55555555</StudyCode>

          </Study>

      </Patient>

</Query>

 

My Code looks like the following:

 

$PAT_ID = '1145566';

 

  $xmlstring = file_get_contents("patients.xml");

  $xml = new SimpleXMLElement($xmlstring);

  foreach ($xml->Patient as $Patient) {

    if (strtolower($Patient->Study->PatientID) == "$PAT_ID") {

        $sid = $Patient->Study->StudyCode . "</p>";

        echo($sid);

    }

  }

 

When I do this, I only get the first StudyCode for each Patient.  I would like to get the StudyCodes for Each Study.  The PatientID in the file will always be the same, therefore I don't need that to actually be the qualifyier.  I just want to get the StudyCode for each Patient and set it as a variable.

 

I am really stumped, but know that I need to not just loop through patients but also loop through the Studies.  I am bad with nested loops, please advise if anyone can help.

 

Rab

Link to comment
Share on other sites

Seeing a visual representation of the data as an array (so you can see the indexes), the way php sees it, will probably help. See the following print_r() code added to your code and I believe the two foreach() loops that do what you want -

 

<?php
$PAT_ID = '1145566';

  $xmlstring = file_get_contents("patients.xml");
  $xml = new SimpleXMLElement($xmlstring);

echo "<pre>";
print_r($xml);
echo "</pre>";

foreach ($xml->Patient as $Patient)
{
foreach($Patient->Study as $Study)
{
	if (strtolower($Study->PatientID) == "$PAT_ID") {
		$sid = $Study->StudyCode . "</p>";
		echo($sid);
	}
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.