aplhabetically ordered dropdown

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smilesmita
Forum Commoner
Posts: 30
Joined: Thu May 24, 2007 1:52 pm

aplhabetically ordered dropdown

Post by smilesmita »

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!

Code: Select all

 
$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++;
}
 
 
 
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: aplhabetically ordered dropdown

Post by flying_circus »

I'm sure there's probably a better / easier way, but the first thing that came to mind was to load the values into an array, sort the array, then print the output.

Code: Select all

$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";
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: aplhabetically ordered dropdown

Post by pickle »

Ya - that's about the best way to do it - the only way as far as I know.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
smilesmita
Forum Commoner
Posts: 30
Joined: Thu May 24, 2007 1:52 pm

Re: aplhabetically ordered dropdown

Post by smilesmita »

doesnt work..its giving nothing in the drop down box..?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: aplhabetically ordered dropdown

Post by flying_circus »

smilesmita wrote:doesnt work..its giving nothing in the drop down box..?
How about doing some troubleshooting to see where it's fouling up? I've only seen 8 lines of your code. The code I wrote was an example to inspire you to write your own working solution.

Try dumping the contents of $attribs:

var_dump($attribs);

Is it getting populated from the while loop?

Is the code outputting any <option> tags in your HTML?
smilesmita
Forum Commoner
Posts: 30
Joined: Thu May 24, 2007 1:52 pm

Re: aplhabetically ordered dropdown

Post by smilesmita »

i did var dump and its giving:
array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { } array(0) { }

html:
<option value="" selected> </select>
Post Reply