smilesmita Posted March 14, 2008 Share Posted March 14, 2008 I have this dropdown which pulls fields from the xml file and displays the names . The list is not alphabetically sorted and I wanted to know how i could do that. following is the code/Please help! $sxml = new SimpleXMLElement(file_get_contents($xmlfile)); $i = 0; $options = '<option value="" selected>'; while (isset($sxml->specialty[$i])) { $attribs = $sxml->specialty[$i]->attributes(); $options .= sprintf('<option value="%d">%s</option>', $attribs['tid'], $attribs['name']) . "\n"; $i++; } /[code] [/code] Quote Link to comment Share on other sites More sharing options...
OkBoy Posted March 14, 2008 Share Posted March 14, 2008 Isn't that just the basic sort command? Something like: $sxml = new SimpleXMLElement(file_get_contents($xmlfile)); sort($sxml); $i = 0; $options = '<option value="" selected>'; while (isset($sxml->specialty[$i])) { $attribs = $sxml->specialty[$i]->attributes(); $options .= sprintf('<option value="%d">%s</option>', $attribs['tid'], $attribs['name']) . "\n"; $i++; } Pretty sure by default sort goes alphabetically. Quote Link to comment Share on other sites More sharing options...
smilesmita Posted March 14, 2008 Author Share Posted March 14, 2008 it dint work??i thought tht would work..may be because $sxml is not an array.. Quote Link to comment Share on other sites More sharing options...
dbillings Posted March 14, 2008 Share Posted March 14, 2008 maybe try exploding it? $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); sort($pieces); Quote Link to comment Share on other sites More sharing options...
smilesmita Posted March 14, 2008 Author Share Posted March 14, 2008 i was trying this..but its not working too: $sxml = new SimpleXMLElement(file_get_contents($xmlfile)); $i = 0; $options = '<option value="" selected>'; while (isset($sxml->specialty[$i])) { $sxml_read = $sxml->specialty[$i]->attributes(); $attribs[$sxml_read['tid']] = $sxml_read['name']; $i++; } asort($attribs); foreach($attribs as $key => $value) { $options .= sprintf('<option value="%d">%s</option>', $key, $value) . "\n"; } Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 14, 2008 Share Posted March 14, 2008 Try this: http://us2.php.net/manual/en/function.xml-parse-into-struct.php Then sort it. 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.