2012-03-09 14 views
1

他の人が書いたこのPHPスクリプトを修正して掃除しました。地元の私のWAMPサーバー上ではアルファベット順に画像がリストされています(名前はすべて001.jpg〜110.jpgです)。ライブLAMPサーバーでは、日付によって整理されていると思います...ファイル名ではありません。それらはすべてJPEG画像ですので、タイプ別にアレンジすることに心配していません。PHP:画像スクリプトにアルファベット順の画像が載っていない

このスクリプトをアルファベット順に並べるにはどうすればよいですか?

function getPictures() 
{ 
global $page, $per_page, $has_previous, $has_next; 

if ($handle = opendir('tour/')) 
{ 
    $lightbox = rand(); 
    echo '<ul id="pictures">'; 

    $count = 0; 
    $skip = $page * $per_page; 

    if ($skip != 0) {$has_previous = true;} 

    while ($count < $skip && ($file = readdir($handle)) !== false) 
    { 
    if (!is_dir($file) && ($type = getPictureType($file)) != '') {$count++;} 
    } 

    $count = 0; 

    while ($count < $per_page && ($file = readdir($handle)) !== false) 
    { 
    if (!is_dir($file) && ($type = getPictureType($file)) != '') 
    { 
    if (!is_dir('thumbs/')) {mkdir('thumbs/');} 
    if (!file_exists('thumbs/'.$file)) {makeThumb('tour/'.$file,$type);} 

    echo '<li><a href="tour/'.$file.'" rel="lightbox['.$lightbox.']">'; 
    echo '<img src="thumbs/'.$file.'" alt="" />'; 
    echo '</a></li>'; 
    $count++; 
    } 
    } 
    echo '</ul>'; 

    while (($file = readdir($handle)) !== false) 
    { 
    if (!is_dir($file) && ($type = getPictureType($file)) != '') 
    { 
    $has_next = true; 
    break; 
    } 
    } 
} 
} 
+1

はソートという、配列内のファイル名を入れて、リストを出力してください。 – Niko

+0

可能な複製[phpでopendir()を使ってディレクトリリストをアルファベット順に並べ替えて表示](http://stackoverflow.com/questions/884974/sort-and-display-directory-list-alphabetically-using-opendir-in-php ) – j08691

+0

私は配列を考えている、そして、これはその質問の欺瞞ではありません。 – John

答えて

0

は、「ライトボックス」機能のように見える...

function getPictures() 
{ 
if ($handle = opendir('tour/')) 
{ 
    global $page, $per_page, $has_previous, $has_next; 
    $lightbox = rand(); 
    echo '<ul id="pictures">'; 
    $count = 0; 
    $skip = $page * $per_page; 

    $file = scandir('tour/'); 
    $images = array(); 

    foreach ($file as $key => $value) 
    { 
    if (!is_dir('tour/'.$value) && ($type = getPictureType('tour/'.$value)) != '') 
    { 
    array_push($images,$value); 
    } 
    } 

    natsort($images); 

    $count = 0; 
    $start = $per_page*$page; 
    $end = $start+$per_page - 1; 

    foreach ($images as $key => $value) 
    { 
    if ($key>=$start && $key<=$end) 
    { 
    echo '<li><a href="tour/'.$value.'" rel="lightbox['.$lightbox.']"><img src="thumbs/'.$value.'" alt="" /></a></li>'; 
    $count++; 
    } 
    } 
    $not_first = $end+1; 
    if ($key>$end) {$has_next = true;} 
    if ($not_first!=$per_page) {$has_previous = true;} 

    echo '</ul>'; 
} 
} 
2

代わりのreaddirを使用して、あなたは、デフォルトではアルファベット順にソートされ、scandirを使用することができます。

デフォルトでは、ソート順は昇順でアルファベット順になります。 オプションのsorting_orderがSCANDIR_SORT_DESCENDINGに設定されている場合、 ソート順は降順でアルファベット順になります。 SCANDIR_SORT_NONEに設定されている場合、結果はソートされません。

scandirは、ファイル名の配列を返します。readdirは、単一のエントリ名を返します。

また、ファイル名を配列に読み込んで、natsortを使用して並べ替えることもできます。ので、ここで私は上記の投稿機能のフル修正版がある場合

// Orders alphanumeric strings in the way a human being would 
natsort($arr); 

Array 
(
    [3] => img1.png 
    [2] => img2.png 
    [1] => img10.png 
    [0] => img12.png 
) 
関連する問題