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++; } ?> Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
mcfmullen Posted December 20, 2010 Author Share Posted December 20, 2010 Thanks for pointing that out! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.