Domanda

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?

È stato utile?

Soluzione 2

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";
    }
}

Altri suggerimenti

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

Simple, use glob.

$files = glob(WAVEFORM_RELATIVE_PATH.'/*.png');
sort($files);
foreach($files as $file)
    echo '<option....>'.$file.'</option>';
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top