0

I have a code that works perfect for me with one exception. I have 2 folders called "1" and "2". Within Folder "1" I have 3 pictures called "image1.jpg" "image2.jpg" and "image3.jpg"

The scenario now is that it prints folder "1" before "2" and it prints out "image1.jpg" before "image2.jpg"

Is it possible by a small edit without adding excessive or code that takes too much calculations to make the selection of the folder + image to the highest number instead of lowest?

Code attached below:

    $directory_name = "pics/";
    $images = glob($directory_name."*/*.jpg");
    foreach($images as $image) 
    { 
        echo '<img src="/'.$image.'">';

    } 
J. Doe
  • 160
  • 11

2 Answers2

3

You could use rsort:

$directory_name = "pics/";
$images = glob($directory_name."*/*.jpg");
rsort($images);
foreach($images as $image) 
{ 
    echo '<img src="/'.$image.'">';

} 
Renaud C.
  • 535
  • 2
  • 14
  • Perfect!!! Thank you mate. I will accept this question soon, im currently locked for 5 minutes. – J. Doe Jun 16 '16 at 16:21
-2

Try this:

$directory_name = "pics/";
$images = glob($directory_name."*/*.jpg");
for($i=count($images);$i>=0;$i--) 
{ 
    echo '<img src="/'.$images[$i].'">';

} 

EDIT: There is actually a big difference that all you downvoters may have overseen with my vs Renauds answer.
My code does not change the array, his does. If the array has named keys they will still be named, but Renauds code will destroy the keys in the array.

Keeping the same index as you start with can be vital in keeping track of the values.

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Thanks for your try bro, but rsort would be a much better choice as stated above :) – J. Doe Jun 16 '16 at 16:56
  • @JDoe sure, if that works for you then go for it. But keep in mind that the key will be reset. That could be a problem in sometimes. My code does not change the array in any way. If you have named keys they will still be named after my code runs, but the code Renaud gave you will not – Andreas Jun 16 '16 at 17:30