mcfmullen Posted December 19, 2010 Share Posted December 19, 2010 Alright, so I have an xml file differences.xml that is being parsed in XML. This is what the xml looks like: <item code="lM" name="dog"> <cost>5000</cost> <Start>12/15/2010</Start> <End>01/13/2011</End> </item> <item code="lF" name="cat"> <cost>5000</cost> <Start>04/15/2010</Start> <End>04/23/2011</End> </item>[/ I want to have the item names (dog, cat) show in a dropdown menu so that I can select these items for editing before storing in my mysql database. This is the php code I have so far: <?PHP $xml = simplexml_load_file("differences.xml"); $object = $xml->xpath("//item"); echo '<SELECT name=object>'; foreach ($object['name'] as $key => $value) { echo '<OPTION value='.$value.'> '.$value; } echo '</select>'; ?> I do have a dropdown list but there are no values inside it (it is empty). Can anyone help me figure out why? I do have this code that does work which lists the items in plaintext (not in a dropdown) so hopefull this will help us out: <?PHP $xml = simplexml_load_file("differences.xml"); $object = $xml->xpath("//item"); $count = count($object); $i = 0; while($i < $count) { echo '<h1>'.$object[$i]['name'].'</h1>'; $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/222156-array-into-dropdown-list/ Share on other sites More sharing options...
wigpip Posted December 19, 2010 Share Posted December 19, 2010 Hi, the option tag should be closed, but that may not be all foreach ($object['name'] as $key => $value) { echo '<option value='.$value.'> '.$value . ' </option>'; } www.wigpip.com.au Link to comment https://forums.phpfreaks.com/topic/222156-array-into-dropdown-list/#findComment-1149351 Share on other sites More sharing options...
BlueSkyIS Posted December 19, 2010 Share Posted December 19, 2010 these are not equivalent: $object['name'] $object[$i]['name'] the second example works because it is accessing the array variable properly. Link to comment https://forums.phpfreaks.com/topic/222156-array-into-dropdown-list/#findComment-1149356 Share on other sites More sharing options...
mcfmullen Posted December 20, 2010 Author Share Posted December 20, 2010 Thanks for pointing that out! Link to comment https://forums.phpfreaks.com/topic/222156-array-into-dropdown-list/#findComment-1149387 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.