title: convert one parent and multiple identical children into xml
body: <p>hi i have a situation where i want to create an xml from some tables in mysql using php, i can get so far no problem, but my issue arises when i have multiple identical named children.</p>
  
  <p>the tables i have are as follows:</p>
  
  <p>bike</p>
  
  <pre><code>+-----+-----------+-----------+---------+
  | Uid | Bike_name | Bike_year | Bike_id |
  +-----+-----------+-----------+---------+
  |   1 | CBR600    |      2001 |      53 |
  |   2 | ZXR750    |      1995 |      27 |
  |   3 | FZR1000   |      1992 |      33 |
  +-----+-----------+-----------+---------+
  </code></pre>
  
  <p>unique_features</p>
  
  <pre><code>+-----+------------------+---------+
  | Uid |     Feature      | Bike_id |
  +-----+------------------+---------+
  |   1 | Micron exhaust   |      27 |
  |   2 | Single seat      |      27 |
  |   3 | Scorpion exhaust |      53 |
  |   4 | Custom paint     |      53 |
  |   5 | L.E.D lights     |      53 |
  |   6 | Induction kit    |      53 |
  +-----+------------------+---------+
  </code></pre>
  
  <p>bike_facts</p>
  
  <pre><code>+-----+-----------------+-----------+--------+---------+
  | Uid | nought_to_sixty | Top_speed | weight | Bike_id |
  +-----+-----------------+-----------+--------+---------+
  |   1 |             3.2 |       160 |    120 |      27 |
  |   2 |               4 |       160 |    150 |      33 |
  |   3 |             3.5 |       150 |    100 |      53 |
  +-----+-----------------+-----------+--------+---------+
  </code></pre>
  
  <p>the output i want will look like:</p>
  
  <pre><code>&lt;Bikes&gt;
    &lt;Bike&gt;
      &lt;Bike_id&gt;53&lt;/Bike_id&gt;
      &lt;Bike_name&gt;CBR600&lt;/Bike_name&gt;
      &lt;Bike_year&gt;2001&lt;/Bike_year&gt;
      &lt;Bike_facts&gt;
          &lt;nought_to_sixty&gt;3.5&lt;/nought_to_sixty&gt;
          &lt;Top_speed&gt;150&lt;/Top_speed&gt;
          &lt;Weight&gt;100&lt;/Weight&gt;
      &lt;/Bike_facts&gt;
      &lt;Unique_features&gt;
          &lt;feature&gt;Scorpion exhaust&lt;/feature&gt;
          &lt;feature&gt;Custom paint&lt;/feature&gt;
          &lt;feature&gt;L.E.D lights&lt;/feature&gt;
          &lt;feature&gt;Induction kit&lt;/feature&gt;
      &lt;/Unique_features&gt;
    &lt;/Bike&gt;
    &lt;Bike&gt;
      &lt;Bike_id&gt;27&lt;/Bike_id&gt;
      &lt;Bike_name&gt;ZXR750&lt;/Bike_name&gt;
      &lt;Bike_year&gt;1995&lt;/Bike_year&gt;
      &lt;Bike_facts&gt;
          &lt;nought_to_sixty&gt;3.2&lt;/nought_to_sixty&gt;
          &lt;Top_speed&gt;160&lt;/Top_speed&gt;
          &lt;Weight&gt;120&lt;/Weight&gt;
      &lt;/Bike_facts&gt;
      &lt;Unique_features&gt;
          &lt;feature&gt;Micron exhaust&lt;/feature&gt;
          &lt;feature&gt;Single seat&lt;/feature&gt;
      &lt;/Unique_features&gt;
    &lt;/Bike&gt;
    &lt;Bike&gt;
      &lt;Bike_id&gt;33&lt;/Bike_id&gt;
      &lt;Bike_name&gt;FZR1000&lt;/Bike_name&gt;
      &lt;Bike_year&gt;1992&lt;/Bike_year&gt;
      &lt;Bike_facts&gt;
          &lt;nought_to_sixty&gt;4&lt;/nought_to_sixty&gt;
          &lt;Top_speed&gt;160&lt;/Top_speed&gt;
          &lt;Weight&gt;150&lt;/Weight&gt;
      &lt;/Bike_facts&gt;
    &lt;/Bike&gt;
  &lt;/Bikes&gt;
  </code></pre>
  
  <p>i can produce:</p>
  
  <pre><code>&lt;bikes&gt;
    &lt;bike bike_id="27"&gt;
      &lt;bike_name&gt;ZXR750&lt;/bike_name&gt;
      &lt;bike_year&gt;1995&lt;/bike_year&gt;
      &lt;bike_facts&gt;
        &lt;nought_to_sixty&gt;3.2&lt;/nought_to_sixty&gt;
        &lt;top_speed&gt;160&lt;/top_speed&gt;
        &lt;weight&gt;120&lt;/weight&gt;
      &lt;/bike_facts&gt;
    &lt;/bike&gt;
    &lt;bike bike_id="33"&gt;
      &lt;bike_name&gt;FZR1000&lt;/bike_name&gt;
      &lt;bike_year&gt;1992&lt;/bike_year&gt;
      &lt;bike_facts&gt;
        &lt;nought_to_sixty&gt;4&lt;/nought_to_sixty&gt;
        &lt;top_speed&gt;160&lt;/top_speed&gt;
        &lt;weight&gt;150&lt;/weight&gt;
      &lt;/bike_facts&gt;
    &lt;/bike&gt;
    &lt;bike bike_id="53"&gt;
      &lt;bike_name&gt;CBR600&lt;/bike_name&gt;
      &lt;bike_year&gt;2001&lt;/bike_year&gt;
      &lt;bike_facts&gt;
        &lt;nought_to_sixty&gt;3.5&lt;/nought_to_sixty&gt;
        &lt;top_speed&gt;150&lt;/top_speed&gt;
        &lt;weight&gt;100&lt;/weight&gt;
      &lt;/bike_facts&gt;
    &lt;/bike&gt;
  &lt;/bikes&gt;
  </code></pre>
  
  <p>using this code:</p>
  
  <pre><code>$mysqli = new mysqli("localhost", "root", "", "Bikes");
  
  if ($mysqli-&gt;connect_errno) 
  {
    echo "Connect failed ".$mysqli-&gt;connect_error;
    exit();
  }
  
  $query = "SELECT  
  b.bike_name,
  b.bike_year,
  b.bike_id,
  f.nought_to_sixty,
  f.top_speed,
  f.weight
  FROM Bike b
  LEFT JOIN Bike_facts f on b.bike_id = f.bike_id";
  
  $bikesArray = array();
  
  if ($result = $mysqli-&gt;query($query)) 
  {
    while ($row = $result-&gt;fetch_assoc()) 
      {
        array_push($bikesArray, $row);
      }
    if(count($bikesArray))
    {
       createXMLfile($bikesArray);
    }
    $result-&gt;free();
  }
  $mysqli-&gt;close();
  
  
  function createXMLfile($bikesArray)
  {
    $filePath = 'bike.xml';
    $dom     = new DOMDocument('1.0', 'utf-8'); 
    $root      = $dom-&gt;createElement('bikes'); 
    for($i=0; $i&lt;count($bikesArray); $i++)
    {
      $bikeid            =  $bikesArray[$i]['bike_id'];  
      $bike_name         =  $bikesArray[$i]['bike_name']; 
      $bike_year         =  $bikesArray[$i]['bike_year']; 
      $nought_to_sixty   =  $bikesArray[$i]['nought_to_sixty']; 
      $top_speed         =  $bikesArray[$i]['top_speed']; 
      $weight            =  $bikesArray[$i]['weight'];
  
      $bike             = $dom-&gt;createElement('bike');
      $bike-&gt;setAttribute('bike_id', $bikeid);
      $bike_name        = $dom-&gt;createElement('bike_name', $bike_name); 
      $bike-&gt;appendChild($bike_name); 
      $bike_year        = $dom-&gt;createElement('bike_year', $bike_year); 
      $bike-&gt;appendChild($bike_year); 
      $bike_facts       = $dom-&gt;createElement('bike_facts');   
      $nought_to_sixty  = $dom-&gt;createElement('nought_to_sixty', 
  $nought_to_sixty); 
      $bike_facts-&gt;appendChild($nought_to_sixty); 
      $top_speed        = $dom-&gt;createElement('top_speed', $top_speed); 
      $bike_facts-&gt;appendChild($top_speed); 
      $weight           = $dom-&gt;createElement('weight', $weight); 
      $bike_facts-&gt;appendChild($weight);
      $bike-&gt;appendChild($bike_facts);
      $root-&gt;appendChild($bike);
    }
    $dom-&gt;appendChild($root); 
    $dom-&gt;save($filePath); 
  } 
  </code></pre>
  
  <p>but am unsure on how to get the unique_features section, can it be done via sql, all my attempts have created a new row for each unique_feature, which obviously i dont want. the only other way i can think is a separate xml and merge the two, which i dont really want either.</p>