按名称对填充的组合框进行排序

时间:2013-06-12 11:59:29

标签: php javascript

我有一段我正在使用的代码,它在一个组合框中填充了某个目录中的文件夹列表:

<?php
echo '<form>';
$path = "images/";
$handle = opendir($path);   
echo "<select style='width:80%' name='URL' onchange='window.location.href=this.form.URL.options[this.form.URL.selectedIndex].value'><option value>Select Folder...</option>";     
while ($file = readdir($handle)) {
    if (substr($file,0,1) != ".") {
    echo "<option value ='/view.php?user=".$file."'>".$file."</option>";
    }   
}
echo '</select></form>';
closedir($handle);  
?>

除了以随机顺序显示列表之外,这个工作正常,有没有办法在组合框中生成列表之前对列表进行排序?

感谢。

1 个答案:

答案 0 :(得分:0)

类似的东西:

while ($file = readdir($handle)) {
    if (substr($file,0,1) != ".") {
        $tmparray[] = $file;
    }   
}
sort($tmparray);
foreach($tmparray as $file)
    echo "<option value ='/view.php?user=".$file."'>".$file."</option>";