Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting a HTML drop down menu array alphabetic

Status
Not open for further replies.

kusarigama

Technical User
Apr 24, 2002
45
0
0
DE
Hi all,

is there a way to sort a HTML Drop down list array alphabetic ?
I tried the following, but it doesn't work !

Can anyone help me ?

Olli

<select name=&quot;position[]&quot;>
<option value=&quot;Artist&quot;<?=$aa;?>>Artist</option>
<option value=&quot;Dancer&quot;<?=$bb;?>>Dancer</option>
<option value=&quot;Webdes&quot;<?=$cc;?>>Webdes</option>
<option value=&quot;Multim&quot;<?=$dd;?>>Multim</option>
</select>

<?PHP
if($_POST['position'])
{
asort($POST['position'])
}
?>
 
With that HTML code, $_POST['position'] will be a single value, not an array. ______________________________________________________________________
TANSTAAFL!
 
...and how should it look like ;). I really tried all - but in vain ...

Olli
 
I'm sorry, I miss-spoke.

With the HTML you have presented, $_POST['position'] will be an array, but an array with only one element. No sorting is necessary.

If your HTML code read:
Code:
<select name=&quot;position[]&quot;
multiple
Code:
>
  <option value=&quot;Artist&quot;>Artist</option>
  <option value=&quot;Dancer&quot;>Dancer</option>
  <option value=&quot;Webdes&quot;>Webdes</option>
  <option value=&quot;Multim&quot;>Multim</option>
</select>

Then $_POST['position'] will be an array which could have more than one element. In that case, sort(), rsort(), asort(), etc., will rearrange the elements.

As far as determining what the array looks like, here is code I would use for inspecting the contents of that array:

Code:
<?php
print '<pre>';
print_r ($_POST['position']);
print '</pre>';
?>
______________________________________________________________________
TANSTAAFL!
 
Thanks ;). Is there also a way to sort it in the Dropdownbox itself.

Sorry to bug you again ;)

Olli
 
No way to do it on the client side.

If you are producing the HTML code from a PHP script, you could put the options in an array, sort the array, then produce your <option>...</option> tags from the array.

______________________________________________________________________
TANSTAAFL!
 
I use this function, that allows me to edit records by specifying the default value as the current stored value.
The array is multi-dimensional (used as db array result) with 'value' and 'option' as the associations.
$source[0]['value']=1;
$source[0]['option']='option 1';

You just need to sort the array prior to calling them. I use this from an SQL query so I use 'order by'

<?php
function drop_down_box_options($variable,$data,$default=null)
{
$output='<select name=&quot;' . $variable . '&quot;>';
for($i=0;$i<sizeof($data);$i++)
{
$output.= '<option value=&quot;';
$output.= $data[$i]['value'];
if ($default==$data[$i]['value'])
{
$output.= '&quot; selected>';
}
else
{
$output.= '&quot;>';
}
$output.= $result[$i]['option'];
$output.= '</option>';
$output.= &quot;\n&quot;;
}
$output='</select>';
return $output;
}
?>




<?php
echo drop_down_box_options('position[]',$option_array);
or
echo drop_down_box_options('position[]',$option_array,$default_selected);
?>

 
Ooh! Just found your answer while looking for something else


I haven't tested it so don't complain.

Quote
-----------
In short, it takes the options and adds them to an array, then removes the options for the select box, sorts the array then adds the sorted array of options to the select box.
-----------
 
It's quiet big but very helpful - Thanks !

olli
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top