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] Link to comment https://forums.phpfreaks.com/topic/96180-aplhabetically-ordered-dropdown/ 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. Link to comment https://forums.phpfreaks.com/topic/96180-aplhabetically-ordered-dropdown/#findComment-492476 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.. Link to comment https://forums.phpfreaks.com/topic/96180-aplhabetically-ordered-dropdown/#findComment-492484 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); Link to comment https://forums.phpfreaks.com/topic/96180-aplhabetically-ordered-dropdown/#findComment-492489 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"; } Link to comment https://forums.phpfreaks.com/topic/96180-aplhabetically-ordered-dropdown/#findComment-492500 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. Link to comment https://forums.phpfreaks.com/topic/96180-aplhabetically-ordered-dropdown/#findComment-492503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.