I'm populating a select element with the following :
<?php
$files2 = opendir(WAVEFORM_RELATIVE_PATH);
while (false!==($READ=readdir($files2))) {
if (in_array(substr(strtolower($READ),-4),array('.png'))) {
echo '<option'.($TRACKS->waveform==$READ ? ' selected="selected"' : '').'>'.$READ.'</option>'."\n";
}
}
closedir($files2);
?>
At the moment it's returning the results in a totally random order. How do I make the list display in alphabetical order?
An easy way is to use scandir
. You can specify a sort order using SCANDIR_SORT_ASCENDING
(0
) or SCANDIR_SORT_DESCENDING
(1
):
$files2 = scandir(WAVEFORM_RELATIVE_PATH, SCANDIR_SORT_ASCENDING);
foreach($files2 as $file) {
if (in_array(substr(strtolower($file), -4), array('.png'))) {
echo '<option'.($TRACKS->waveform==$file? ' selected="selected"' : '').'>'.$file.'</option>'."\n";
}
}