简体   繁体   中英

Ordering results by most recently added

Im using this code to list all the images in a folder on the page, the class "results" holds the 4 images per paginated page. I can only get them to list in alphabetical order, how can I get them by most recently added?

<?php                           
    $files = glob("upload_image/*.*");

    for ($i = 0; $i < count($files); $i += 4) {
        $colCnt++;
        if ($colCnt == 1) {
            $num = $files[$i];
        }

        echo '<div class="result"><img src="' . $files[$i] . 
                             '" /><img src="' . $files[$i + 1] . 
                             '" /><img src="' . $files[$i + 2] . 
                             '" /><img src="' . $files[$i + 3].'" /> ';
        echo'</div>';

        if ($colCnt == 1) {      
            $colCnt = 0;
        }
    } 
?>

Using filemtime over the results will get you all their times and then you can simply sort that array based upon that time.

<?php
 $files = glob("upload_image/*.*");
 usort($files, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
?>

Credit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM