1

This code is sorting the images alphabetically. How to do sorting by date? So users will be more convenient to view the new image.

Here is the code:

<link href="assets/css/gallery.css" rel="stylesheet">
<link href="assets/css/dope.css" rel="stylesheet">
<div class="row center">
    <ul class="uk-tab" id="gamemodelistregion" uk-switcher="connect: + ul; animation: uk-animation-fade">
    <li aria-expanded="true" class="uk-active">
        <a>All skins</a>
        <?php
            # Skin directory relative to include/gallery.php (this file)
            $skindir = "../skins/";

            # Skin directory relative to index.html
            $skindirhtml = "./skins/";

            $images = scandir($skindir);

            foreach($images as $curimg) {
                if (strtolower(pathinfo($curimg, PATHINFO_EXTENSION)) == "png") {
        ?>
        <li class="skin" onclick="$('#nick').val('{' + $(this).find('.title').text() + '} ');" data-dismiss="modal">
            <div class="circular" style='background-image: url("./<?php echo $skindirhtml.$curimg ?>")'></div>
            <h4 class="title"><?php echo pathinfo($curimg, PATHINFO_FILENAME); ?></h4>
        </li>
        <?php
                }
            }
        ?>
    </li>
</ul>
</div>
TopoR
  • 75
  • 6
  • 1
    Possible duplicate of [scandir() to sort by date modified](https://stackoverflow.com/questions/11923235/scandir-to-sort-by-date-modified) – Gerrit Fries Aug 28 '17 at 16:37

1 Answers1

1

The filemtime function returns a Unix timestamp of the file's last modification date.

// retrieve all images but don't waste time putting them in alphabetical order
$images = scandir($skindir, SCANDIR_SORT_NONE);

// remove all non-existent files and non-PNG files
$images = array_filter($images,
  function($file) use ($skindir) {
    $path = "$skindir/$file";
    return file_exists($path) && strtolower(pathinfo($path, PATHINFO_EXTENSION)) == "png";
  }
);

// sort image by modification time in descending order
usort($images, function($a,$b){return filemtime($a)-filemtime($b);});

// now you can iterate through images and print them
foreach($images as $curimg): ?>
  <!-- Output image HTML here -->
<?php
endforeach;
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
  • Not working. There are no errors, the images and titles are not displayed. – TopoR Aug 28 '17 at 16:54
  • @salathe why did you make this edit? In the OP code -- which is actually displaying images, just in the wrong order -- the pathinfo function is fed each member of `$images`. That's what I was doing – BeetleJuice Aug 28 '17 at 16:59
  • @TopoR please try now. – BeetleJuice Aug 28 '17 at 17:01
  • I added the code and here's how it works here http://agarix.esy.es/include/gallery.php – TopoR Aug 28 '17 at 17:05
  • @BeetleJuice because `scandir()` returns the list of file (and directory) names only; the `file_exists()` and `pathinfo()` functions will not be looking at the scanned directory if you only pass them the names. I suspect the OP's code was "working" only by chance or coincidence, if it was working at all. – salathe Aug 28 '17 at 17:06
  • @TopoR I've implemented the change that salathe suggested. See whether that helps. If not, I can't really do debugging for you from here. You'll need to walk through code execution in a debugger or use `var_dump($variable)` at different points to see what's going on. You may also need to look in your browser's dev tools to see what the HTML looks like, and whether network requests for the images are correct. – BeetleJuice Aug 28 '17 at 17:10
  • @BeetleJuice I added the code and now it is not working correctly http://agarix.esy.es/include/gallery.php – TopoR Aug 28 '17 at 17:11
  • Warning: filemtime(): stat failed for kr.png in C:\xampp\htdocs\include\gallery.php on line 27 – TopoR Aug 28 '17 at 17:13
  • @TopoR That error refers to [the stat() function](http://php.net/manual/en/function.stat.php) which is used internally by `filemtime()`. I don't know why it failed so you'll need to research why `stat()` fails on your Windows system, or find another way to get last modified time. – BeetleJuice Aug 28 '17 at 17:23
  • @BeetleJuice hi, had to change the 'filemtime' with '@filemtime' to make it work on Windows. Now, the sorting is not working properly, sorting on site and Windows for the date are very different. It is possible to fix it? http://agarix.esy.es/include/gallery.php – TopoR Aug 28 '17 at 23:08
  • @BeetleJuice on the website in the beginning is: poland pokerface polarbear portugal powerbadger ... and on Windows: cursedblade gladiatrix helm orcgrunt orcwarrior – TopoR Aug 28 '17 at 23:11