title: Parsing XML document with PHP using 'foreach' loop
body: <p>I'm new to PHP, MySQL and XML... and have been trying to wrap my head around classes, objects, arrays and loops. I'm working on a parser that extracts data from an XML file, then stores it into a database. A fun and delightfully frustrating challenge to work on during the christmas holiday.</p>
  
  <p>Before posting this question I've gone over the PHP5.x documentation, W3C and also searched quite a bit around stackoverflow.</p>
  
  <p>Here's the code...</p>
  
  <h2> > XML:</h2>
  
  <pre><code>&lt;alliancedata&gt;
      &lt;server&gt;
          &lt;name&gt;irrelevant&lt;/name&gt;
      &lt;/server&gt;
  
      &lt;alliances&gt;
          &lt;alliance&gt;
              &lt;alliance id="101"&gt;Knock Out&lt;/alliance&gt;
  
              &lt;roles&gt;
                  &lt;role&gt;
                      &lt;role id="1"&gt;irrelevant&lt;/role&gt;
                  &lt;/role&gt;
              &lt;/roles&gt;
  
              &lt;relationships&gt;
                  &lt;relationship&gt;
                      &lt;proposedbyalliance id="102" /&gt;
                      &lt;acceptedbyalliance id="101" /&gt;
                      &lt;relationshiptype id="4"&gt;NAP&lt;/relationshiptype&gt;
                      &lt;establishedsince&gt;2014-12-27T18:01:34.130&lt;/establishedsince&gt;
                  &lt;/relationship&gt;
                  &lt;relationship&gt;
                      &lt;proposedbyalliance id="101" /&gt;
                      &lt;acceptedbyalliance id="103" /&gt;
                      &lt;relationshiptype id="4"&gt;NAP&lt;/relationshiptype&gt;
                      &lt;establishedsince&gt;2014-12-27T18:01:34.130&lt;/establishedsince&gt;
                  &lt;/relationship&gt;
                  &lt;relationship&gt;
                      &lt;proposedbyalliance id="104" /&gt;
                      &lt;acceptedbyalliance id="101" /&gt;
                      &lt;relationshiptype id="4"&gt;NAP&lt;/relationshiptype&gt;
                      &lt;establishedsince&gt;2014-12-27T18:01:34.130&lt;/establishedsince&gt;
                  &lt;/relationship&gt;
              &lt;/relationships&gt;
          &lt;/alliance&gt;
  &lt;/alliancedata&gt;
  </code></pre>
  
  <h2> > PHP:</h2>
  
  <pre><code>$xml = simplexml_load_file($alliances_xml); // $alliances_xml = path to file
  
    // die(var_dump($xml));
    // var_dump prints out the entire unparsed xml file.
  
    foreach ($xml-&gt;alliances as $alliances) {
  
         // Alliance info 
         $alliance_id = mysqli_real_escape_string($dbconnect, $alliances-&gt;alliance-&gt;alliance['id']);
         $alliance_name = mysqli_real_escape_string($dbconnect,$alliances-&gt;alliance-&gt;alliance);
  
         // Diplomacy info
         $proposed_by_alliance_id = mysqli_real_escape_string($dbconnect,$alliances-&gt;alliance-&gt;relationships-&gt;relationship-&gt;proposedbyalliance['id']);
         $accepted_by_alliance_id = mysqli_real_escape_string($dbconnect,$alliances-&gt;alliance-&gt;relationships-&gt;relationship-&gt;acceptedbyalliance['id']);
         $relationship_type_id = mysqli_real_escape_string($dbconnect,$alliances-&gt;alliance-&gt;relationships-&gt;relationship-&gt;relationshiptype['id']);
         $established_date = mysqli_real_escape_string($dbconnect,$alliances-&gt;alliance-&gt;relationships-&gt;relationship-&gt;establishedsince);
  
  // this is my attempt to echo every result
  echo "Alliance ID: &lt;b&gt;$alliance_id&lt;/b&gt; &lt;br/&gt;";
  echo "Alliance NAME: &lt;b&gt;$alliance_name&lt;/b&gt; &lt;br/&gt;";
  echo "Diplomacy Proposed: &lt;b&gt;$proposed_by_alliance_id&lt;/b&gt; &lt;br/&gt;";
  echo "Diplomacy Accepted: &lt;b&gt;$accepted_by_alliance_id&lt;/b&gt; &lt;br/&gt;";
  echo "Diplomacy Type: &lt;b&gt;$relationship_type_id&lt;/b&gt; &lt;br/&gt;";
  echo "Date Accepted: &lt;b&gt;$established_date&lt;/b&gt; &lt;br/&gt;";
  echo "&lt;hr/&gt;";
  }
  </code></pre>
  
  <h2> > intrepter output:</h2>
  
  <pre><code>Alliance ID: 1 
  Alliance NAME: Knock Out
  Diplomacy Proposed: 102 
  Diplomacy Accepted: 101
  Diplomacy Type: 4 
  Date Accepted: 2011-10-24T05:08:35.830
  </code></pre>
  
  <hr>
  
  <p>I don't understand why the loop simply stops after parsing the first row of data. My best guess, is that my code is not telling PHP what to do after the first values are parsed.</p>
  
  <p><em>Honestly I have no idea how to explain this in words, so here's a visual representation.</em></p>
  
  <p>First row is interpreted as</p>
  
  <pre><code>---&gt;$alliance_id
  ---&gt;$alliance_name
  ---&gt;$proposed_by_alliance_id
  ---&gt;$accepted_by_alliance_id
  ---&gt;$relationship_type_id
  ---&gt;$established_date
  </code></pre>
  
  <p>then for the next <code>&lt;relationship&gt;</code> subnodes the following happens...</p>
  
  <pre><code>---&gt; ?? _(no data)_
  ---&gt; ?? _(no data)_
  ---&gt;$proposed_by_alliance_id
  ---&gt;$accepted_by_alliance_id
  ---&gt;$relationship_type_id
  ---&gt;$established_date
  </code></pre>
  
  <p>Since I'm not telling PHP to add <code>$alliance_id</code> and <code>$alliance_name</code> to every iteration of the <code>&lt;relationship&gt;</code> subnode, the interpreter simply decides to abort the <code>foreach</code> operation. 
  As I mentioned above, I'm new to both PHP and Stackoverflow and I really appreciate any help or wisdom you can share. Thank you in advance.</p>