MariusB
MariusB

Reputation: 37

populating dropdown with values from xml file in php

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>

Upvotes: 1

Views: 2848

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94672

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>

Upvotes: 2

Related Questions