0

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?

Grant
  • 1,297
  • 2
  • 16
  • 39

3 Answers3

1

May be you can store the filename in an array, sort the array and then use them in select options

Lepanto
  • 1,413
  • 1
  • 8
  • 15
0

Simple, use glob.

$files = glob(WAVEFORM_RELATIVE_PATH.'/*.png');
sort($files);
foreach($files as $file)
    echo '<option....>'.$file.'</option>';
sectus
  • 15,605
  • 5
  • 55
  • 97
0

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";
    }
}
user428517
  • 4,132
  • 1
  • 22
  • 39
  • Thanks for the suggestion but this doesn't return any results in my drop down list. Empty dir? It may be because the original routine is reading which option has been previously selected and stored and this one doesn't look like it is? Or maybe I'm wrong? – Grant Jun 04 '13 at 17:56
  • this was just an example to show how `scandir` works. i don't know the purpose of your code or how it's supposed to work. maybe try echoing out `$file` in the loop to make sure the files in the directory are actually being read into `$file2`. – user428517 Jun 04 '13 at 18:19
  • 1
    Brilliant, that did the job. Thanks so much ;) – Grant Jun 04 '13 at 18:21