开发者

I need to sort php jquery gallery script alphabetically

know nothing about php, but I have this script that reads a folder and displays a thumbnail gallery, proble开发者_运维知识库m 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);


Try using glob instead of opendir, something like:

$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 should return sorted list of files.

EDIT: Thanks to Doug Neiner for a tip ;)


add the sort line as given below

$file_parts = explode('.',$file);
sort($file_parts) or die("sorting failed");
$ext = strtolower(array_pop($file_parts));


It's probably easiest to put the values from the files into an array, then sort the array before outputting it to the page.


@Habicht The Code works fine, but the proper thumbnails are not working anymore since the thumbnails directory reference is removed :

So I tried like this :

$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++;
  }
}

The Thumbnails works fine, but the reference image is always the same for any thumbnail - the first image file in $directory. I'm sure there is some other way to combine these foreach statements to fix all at once.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜