0

know nothing about php, but I have this script that reads a folder and displays a thumbnail gallery, problem is it dosent display alphabetically. Have searched the net and seen that sort does this but have no idea where to start any help would be much appreciated.

heres the script

$sitename = $row_wigsites['id'];     
$directory = 'sites/'.$sitename.'/pans';
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;

$dir_handle = @opendir($directory) or die("There is an error with your image directory!");

while ($file = readdir($dir_handle)) 

{

if($file=='.' || $file == '..') continue;

$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));

$title = implode('.',$file_parts);
$title = htmlspecialchars($title);


$nomargin='';

if(in_array($ext,$allowed_types))
{

    if(($i+1)%4==0) $nomargin='nomargin';

    echo '
    <div class="pic '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;">
    <a href="'.$directory.'/'.$file.'" title="Panoramic Stills taken at '.$title.'°" rel="pan1" target="_blank">'.$title.'</a>
    </div>';

    $i++;
}
}

closedir($dir_handle);
4

4 に答える 4

1

次のように、opendir の代わりにglobを使用してみてください。

$i=0;
foreach (glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file){
    if($file=='.' || $file == '..') continue;
    $file_parts = explode('.',$file);
    $ext = strtolower(array_pop($file_parts));
    $title = basename($file);
    $title = htmlspecialchars($title);
    $nomargin='';
    if(($i+1)%4==0) $nomargin='nomargin';
    echo '
    <div class="pic '.$nomargin.'" style="background:url('.$file.') no-repeat 50% 50%;">
    <a href="'.$file.'" title="Panoramic Stills taken at '.$title.'°" rel="pan1" target="_blank">'.$title.'</a>
    </div>';
    $i++;
}

Glob は、ソートされたファイルのリストを返す必要があります。

編集: Doug Neiner のヒントに感謝 ;)

于 2010-01-18T16:03:06.960 に答える
0

@Habichtコードは正常に機能しますが、サムネイルディレクトリ参照が削除されたため、適切なサムネイルが機能しなくなりました。

だから私はこのように試しました:

$i=0;
foreach (glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file)
{
  foreach (glob($thumbs_directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file2)
  {
     if($file=='.' || $file == '..') continue;
     $file_parts = explode('.',$file);
     $ext = strtolower(array_pop($file_parts));
     $title = basename($file);
     $title = htmlspecialchars($title);
     $title = str_replace("_"," ",$title);
     $nomargin='';
     if(($i+1)%4==0) $nomargin='nomargin';
     echo '<div class="pic '.$nomargin.'" style="background:url('.$file2.') no-repeat 50% 50%;">
       <a href="'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
     </div>';
     $i++;
  }
}

サムネイルは正常に機能しますが、参照画像はどのサムネイルでも常に同じです。つまり、$directoryの最初の画像ファイルです。これらのforeachステートメントを組み合わせて一度に修正する方法は他にもあると思います。

于 2013-03-10T22:12:10.587 に答える
0

ファイルの値を配列に入れ、ページに出力する前に配列をソートするのがおそらく最も簡単です。

于 2010-01-18T16:02:36.807 に答える
0

以下のようにソート行を追加します

$file_parts = explode('.',$file);
sort($file_parts) or die("sorting failed");
$ext = strtolower(array_pop($file_parts));
于 2010-01-18T15:59:58.090 に答える