Jump to content

Need help with array_merge(glob...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
amir2000

amir2000

    Newbie

  • Members
  • PipPip
  • 13 posts
I have the following code:

I'm displaying photos from the thumbnails folder and having a link to the photo in different folder.
The problem is that it's displaying all the photos but making a mess with the links.
Example:
It's showing photo # 1 and link it to photo # 3.

How can I solve that?

Any help will be welcomed.

Thanks,

Amir

<?php
$files = array_merge(glob("photos/nature/*.jpg"), glob("photos/nature/*.JPG"), glob("photos/nature/*.JPEG"), glob("photos/nature/*.jpeg")); for ($i=0; $i<count($files); $i++) {  
$files_1 = array_merge(glob("photos/thumbnails/nature/*.jpg"), glob("photos/thumbnails/nature/*.JPG"), glob("photos/thumbnails/nature/*.JPEG"), glob("photos/thumbnails/nature/*.jpeg")); for ($i=0; $i<count($files); $i++) {
$num = $files[$i];
$num_1 = $files_1[$i];  
echo '<li><a href="'.$num.'"><img src="'.$num_1.'" alt=" " width="120" height="120" /></a></li>';  
}}?>


#2
amir2000

amir2000

    Newbie

  • Members
  • PipPip
  • 13 posts
Found the problem:
The file extensions were not the same in the two folders :rolleyes:
Now it's working correctly.

Now i would like to know if there is any way to sort it by date that the photo added or any other sorting criteria.

Thanks,

Amir

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
You can loop through the array and use filemtime() on each image to get the date it was last modified (created). You could also read EXIF to get the image's creation date, may be another solution: PHP: exif_read_data - Manual
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
amir2000

amir2000

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi again,

Is it possible to get an example? (sorry just starting to understand php...)

Thanks,

Amir

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
This should work, I had cleaned up your code and commented on what I had done. This assumes that thumbnails have the same name as the image, which it should be:
$files = array_merge(glob("photos/nature/*.jpg"), glob("photos/nature/*.JPG"), 
                     glob("photos/nature/*.JPEG"), glob("photos/nature/*.jpeg")); 

//array of times modified
$modified = array();

//assign last modified time to array
foreach($files as $file) {
    $modified[] = filemtime($file);
}


//combine into single array, photo => modified time
$combined = array_combine($files, $modified);

//sort by key
ksort($files);

//simple foreach loop
foreach($files as $photo) {
    //replace thumbnails to thumbnails directory
    $thumbnail = str_replace('photos/nature/', 'photos/thumbnails/nature/', $photo);
    echo '<li><a href="'.$photo.'"><img src="'.$thumbnail.'" alt=" " width="120" height="120" /></a></li>';  
}
And it will successfully display the photos based on their modified time, or creation date.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
amir2000

amir2000

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi,

Thank you very much but it still doesn't order them by date created.

Amir

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Try to replace
[COLOR=#000000][COLOR=#ff8000]//assign last modified time to array
[/COLOR][COLOR=#007700]foreach([/COLOR][COLOR=#0000bb]$files [/COLOR][COLOR=#007700]as [/COLOR][COLOR=#0000bb]$file[/COLOR][COLOR=#007700]) {
    [/COLOR][COLOR=#0000bb]$modified[/COLOR][COLOR=#007700][] = [/COLOR][COLOR=#0000bb]filemtime[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$file[/COLOR][COLOR=#007700]);
}[/COLOR][/COLOR]
with
//assign last modified time to array
foreach($files as $file) {
    $exif_data = exif_read_data($file);
    $modified[] = $exif_data['DateTime'];
}
This is the only other way, as there is no way for PHP to determine the creation date of the file, as they were copied to your web server.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
amir2000

amir2000

    Newbie

  • Members
  • PipPip
  • 13 posts
Hi,

Thanks again but it's still sorting the photo by the alphabet (first capitals).

Amir