Add all the options to an array first:
$itemOptions = array();
while ($y <= $totalItemsAvail) #Loops based on the number of items needed.
{
$itemOptions[] = getItemNames($y);
$y++;
}
Use asort()
to sort them alphabetically while preserving the keys. Then use a foreach
to loop through the array and display the <option>
s:
asort($itemOptions);
foreach ($itemOptions as $value => $option) {
echo sprintf('<option value="%d">%s</option>', $value, $option);
}