Jump to content

Loop Through XML with PHP


s_ainley87

Recommended Posts

Hi There!

 

I currently have an XML file that looks like this.....

 

<menuBase>

      <country>

          England

      </country>

     

      <country>

          France

      </country>

 

    <country>

          Germany

      </country>

</menuBase>

 

how would I loop through that file and get the results i.e England, France, Germany into a Drop Down Form Menu?

Link to comment
https://forums.phpfreaks.com/topic/114060-loop-through-xml-with-php/
Share on other sites

SimpleXML is the way to go.

 

<?php
  $file = 'filename.xml';
  $sxml = simplexml_load_file($file);
  echo "<select name=\"country\">";
  foreach($sxml->country as $country)
    echo "<option value=\"{$country}\">{$country}</option>";
  echo "</select>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.