Page 1 of 1
aplhabetically ordered dropdown
Posted: Fri Mar 14, 2008 11:36 am
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++;
}
Re: aplhabetically ordered dropdown
Posted: Fri Mar 14, 2008 1:51 pm
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";
}
Re: aplhabetically ordered dropdown
Posted: Fri Mar 14, 2008 2:25 pm
by pickle
Ya - that's about the best way to do it - the only way as far as I know.
Re: aplhabetically ordered dropdown
Posted: Fri Mar 14, 2008 2:35 pm
by smilesmita
doesnt work..its giving nothing in the drop down box..?
Re: aplhabetically ordered dropdown
Posted: Fri Mar 14, 2008 3:02 pm
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?
Re: aplhabetically ordered dropdown
Posted: Fri Mar 14, 2008 3:42 pm
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>