Jump to content

aplhabetically ordered dropdown


smilesmita

Recommended Posts

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

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.

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";

  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.