0

这是我的代码。但是 while 循环中的 read(): 似乎并没有按顺序对我的结果进行排序。有没有办法对我的结果进行排序?tks 塞比

 <?php
 $save_path_folder_images = '../simplegallery/users/'.$_SESSION['user_id'].'       /'.$_REQUEST['gal_folder'].'/thumbs/';
 $save_path_folder_images_full = '/simplegallery/users/'.$_SESSION['user_id'].'/'.$_REQUEST['gal_folder'].'/slides/';
$folder=dir($save_path_folder_images);

$counter = 1;
while($folderEntry=$folder->read())
{
    if($folderEntry!="." && $folderEntry!="..")
    {?>
         <div class="imgs" >
                <div class="thumb" >                        
                        <label for="img<?php echo $counter; ?>"><img class="img" src="<?php echo $save_path_folder_images.$folderEntry; ?>" /></label>                          
                    </div>
                <div class="inner">
                    <input type="checkbox" class="chk " id="img<?php echo $counter; ?>" name="img_name[]" value="<?php echo $save_path_folder_images_full.$folderEntry; ?>" />
                </div>
            </div>
        <?php
    $counter++;
    }
}
  ?>
4

2 回答 2

1

->read()不尝试对输出进行排序,您可以循环read()一次,将输出推送到数组中。然后通过natcasesort()( http://php.net/natcasesort ) 运行它以对数组进行排序,然后将其打印出来。

所以两个循环。

while($folderEntry=$folder->read())
{
  $fileList[]  = $folderEntry;
}
natcasesort($fileList);
foreach($fileList as $folderEntry)
{
  //your printing
}
于 2013-05-25T14:05:17.370 回答
0

你为什么不使用DirectoryIterator

$files = array();
$dir = new DirectoryIterator($save_path_folder_images);
foreach ($dir as $fileinfo) {     
   if ($fileinfo->isFile()) {
       $files[] = $fileinfo->getFilename();
   }
}

natcasesort($files);

检查这个线程以及使用 DirectoryIterator 对文件进行排序

于 2013-05-25T14:25:50.260 回答