简体   繁体   中英

PhP foreach loop for an XML file

I'm trying to be able to get place as a variable to use on page.php. It will start to load options into the select box for about 5 seconds and I'll actually be able to see the options, then it all deletes and I have an empty select box. Here's the PHP code:

<?php
$xml = simplexml_load_file("file.xml");
echo '<form method="get" action="page.php"><select name="place" id="place">';
foreach($xml as $banana)
{
    if(isset($banana->id))
    {
        $id = $banana->id;
        $string = $banana->state . " - " . $banana->name;
        echo '<option value="' . $id . '">' . $string . '</option>';

    }
}
echo "</select>";
echo '<input type="submit" /></form>';

?>

I know for sure that my XML is formatted correctly, but the XML file has a little over 2500 entries that each looks something like this:

<container>
    <id>theId</id>
    <state>theState</state>
    <name>theName</name>
</container>

Any ideas why won't load?

Note: Some items have an empty id in the XML file, and I don't want to include those files, that's why I check to make sure it's set.

A couple things

  • Disable any javascript and try again--something may be binding to overriding the select box or the id of an element in it.
  • You generally want to properly escape text you pull in before inserting it into a HTML document in case there's a < , > , or such in it

Try the following:

   echo '<option value="' . htmlspecialchars($id) . '">' . htmlspecialchars($string) . '</option>'; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM