Script to list images in descending form (from last to first)

Asked

Viewed 26 times

0

Hello. I have this script that shows a list of images within a certain folder, and allows you to choose the amount of images to show per page. But if the files have the names 1.jpg 2.jpg 3.jpg , it shows followed in increasing form, 1,2,3... but what I intended was to start with the last... 3,2,1... How can I do that?

  <?php

        $folder = 'test/';
        $filetype = '*';    
        $files = glob($folder.$filetype);    
        $total = count($files);    
        $per_page = 12;    
        $last_page = ceil($total / $per_page);  

        if (strpos($last_page, '.') !== false) {
        $last_page = $last_page;
        }

        if(isset($_GET["page"])  && ($_GET["page"] <=$last_page) && ($_GET["page"] > '0') ){
            $page = $_GET["page"];
            $offset = $per_page*($page - 1);      
        }else{
            $page=1;
            $offset=0;      
        }    
        $max = $offset + $per_page;    
        if($max>$total){
            $max = $total;
        }

            show_pagination($page, $last_page);    

            for($i = $offset; $i< $max; $i++){
             $file = $files[$i];
        $path_parts = pathinfo($file);
        $filename = $path_parts['filename'];        
        echo '        
        <div class="galleryCellHolder">
            <div class="galleryCell">
                <a class="fancybox" rel="group" href="'.$file.'"><img class="galleryPhoto" src="'.$file.'" alt="'.$filename.'"></a>
            </div>
        </div>        
        ';          
}

        function show_pagination($current_page, $last_page){
        echo '<div><p>';
            if( $current_page > 1 ){
                echo '<a href="?page='.($current_page-1).'">Anterior</a>';
            }
            echo '&nbsp;&nbsp;&nbsp;&nbsp;';
            if( $current_page < $last_page ){
                echo '<a href="?page='.($current_page+1).'">Próximo</a>';  
            }
            echo '</div></p>';

        }

        echo '<p>'.show_pagination($page, $last_page).'</p>';

        echo "<b>Página $page de $last_page</b>";

        ?>

1 answer

0

I’ve already found.... I use reverse mode ;)

$files = array_reverse(glob($Folder. $filetype));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.