1

I want to make a dropdown list with the values from inside the xml file. The dropdown is present but it is blank on the web. Why? Is there something i've missed?

I have the following code:

<form method="post" action="">
    <?php
    echo "<select>";
        $xml = simplexml_load_file('curs.xml');
            foreach ($xml->item as $item)
            {
                echo "<option value='".$item->name."'></option>";
            }
    echo "</select>";
    ?>
</form>

The code for the xml file:

<prod>

<item>
    <name>Cheese</name>
    <price>4.25</price>
</item>
<item>
    <name>Milk</name>
    <price>8.12</price>
</item>
<item>
    <name>Egg</name>
    <price>0.81</price>
</item>

</prod>
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
MariusB
  • 37
  • 1
  • 2
  • 7
  • You don't have anything in the option. ` – Brett Santore Jun 22 '14 at 16:36
  • Works here: http://codepad.org/iEug9Lc4 You must have a path problem to the xml file. Use the absolute path, e.g. `/home/sites/www/resources/curs.xml` (or whatever it is). EDIT: I've just noticed you're not printing the values *within* the ` – Jared Farrish Jun 22 '14 at 16:38

1 Answers1

2

Try this

foreach ($xml->item as $item)
{
   echo "<option value='".$item->name."'>" . $item->name . "</option>";
}

The visible part of an <option> is what you put between <option> and </option>

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149