Jump to content

PHP Gallery - Sorting newest images first


Kimzerx

Recommended Posts

PHP Gallery

Found this neat php gallery, but im trying to get it to display the newest pictures first. The files are already being named by date so i think this should be doable. By newest picture i mean by creation time. And i would really appreciate some help here.

Found some posts about it referring to this function, but im struggling to incorporate it.

 

function get_files($images_dir,$exts = array('jpg')) {
$files = array();
$times = array();
if($handle = opendir($images_dir)) {
    while(false !== ($file = readdir($handle))) {
        $extension = strtolower(get_file_extension($file));
        if($extension && in_array($extension,$exts)) {
            $files[] = $file;
            $times[] = filemtime($images_dir . '/' . $file);
        }
    }
    closedir($handle);
}
array_multisort($files, SORT_DESC, $times);
return $files;
}

 

Link to comment
Share on other sites

Instead of

$files[] = $file;
$times[] = filemtime($images_dir . '/' . $file);

have a single array

$files[] = [   filemtime($images_dir . '/' . $file),
               $file
           ];

Then all you need is a simple descending sort

rsort($files);

which will sort on the first element of each subarray.

Link to comment
Share on other sites

Like this?

 

<!DOCTYPE html>
<html lang="en-GB">
	<head>

		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

		<link href="_assets/magnifix-popup/magnific-popup.css" rel="stylesheet" type="text/css"/>
		<link href="_assets/css/main.css" rel="stylesheet" type="text/css"/>

		<title>Kimz Party Snapshots</title>

  </head>
  <body>

		<section class="qt-photo-gallery">

			<?php
			function get_files($qt_folder_name,$exts = array('jpg')) {
			$files = array();
			$times = array();
			if($handle = opendir($qt_folder_name)) {
				while(false !== ($file = readdir($handle))) {
					$extension = strtolower(get_file_extension($file));
					if($extension && in_array($extension,$exts)) {
					$files[] = [   filemtime($qt_folder_name . '/' . $file),
								   $file
							   ];
					}
				}
				closedir($handle);
			}
			array_multisort($files, SORT_DESC, $times);
			return $files;
			}
/* ========================================================================== */

			$qt_folder_name = 'images'; // change this to a folder of your choice for images
			$qt_handle = opendir( dirname( realpath( __FILE__ ) ) . '/' . $qt_folder_name . '/' );

/* ========================================================================== */

			$qt_counter = 0;

			while( $qt_file = readdir( $qt_handle ) ) :

				if( $qt_file !== '.' && $qt_file !== '..' ) :

					$qt_counter++;

					$qt_file_path = $qt_folder_name . '/' . $qt_file;
					
					rsort($files);
					
					?>

<!-- ======================================================================= -->

					<div class="qt-photo-gallery-item qt-image-no-<?php echo $qt_counter; ?>">
						<a class="qt-photo-gallery-item-link" href="<?php echo $qt_file_path; ?>" title="File name: <?php echo $qt_file; ?>">

							<div class="qt-photo-gallery-item-image-wrapper">

								<img src="<?php echo $qt_file_path; ?>" class="qt-photo-gallery-item-image" />

							</div>

						</a>
					</div>

<!-- ======================================================================= -->

					<?php

					if( $qt_counter == 5 ) {
						$qt_counter = 0;
					}

				endif;

			endwhile;

/* ========================================================================== */

			?>

		</section>

		<script src="_assets/js/jquery-3.2.1.min.js" type="text/javascript"></script>
		<script src="_assets/magnifix-popup/jquery.magnific-popup.min.js" type="text/javascript"></script>
		<script src="_assets/js/main.js" type="text/javascript"></script>

  </body>
</html>

 

Link to comment
Share on other sites

Alternatively

function get_files($qt_folder_name,$exts = array('jpg')) {
    $files = array();
    if($handle = opendir($qt_folder_name)) {
        while(false !== ($file = readdir($handle))) {
            $extension = strtolower(get_file_extension($file));
            if($extension && in_array($extension,$exts)) {
                $files[$file] = filemtime($qt_folder_name . '/' . $file);
            }
        }
        closedir($handle);
    }
    arsort($files);
    return array_keys($files);
}

 

Link to comment
Share on other sites

Did try your alternative, but im not getting it to work.

I must be missing something.

 

<!DOCTYPE html>
<html lang="en-GB">
	<head>

		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

		<link href="_assets/magnifix-popup/magnific-popup.css" rel="stylesheet" type="text/css"/>
		<link href="_assets/css/main.css" rel="stylesheet" type="text/css"/>

		<title>Kimz Party Snapshots</title>

  </head>
  <body>

		<section class="qt-photo-gallery">

			<?php
			function get_files($qt_folder_name,$exts = array('jpg')) {
				$files = array();
				if($handle = opendir($qt_folder_name)) {
					while(false !== ($file = readdir($handle))) {
						$extension = strtolower(get_file_extension($file));
						if($extension && in_array($extension,$exts)) {
							$files[$file] = filemtime($qt_folder_name . '/' . $file);
						}
					}
					closedir($handle);
				}
				arsort($files);
				return array_keys($files);
			}
/* ========================================================================== */

			$qt_folder_name = 'images'; // change this to a folder of your choice for images
			$qt_handle = opendir( dirname( realpath( __FILE__ ) ) . '/' . $qt_folder_name . '/' );

/* ========================================================================== */

			$qt_counter = 0;

			while( $qt_file = readdir( $qt_handle ) ) :

				if( $qt_file !== '.' && $qt_file !== '..' ) :

					$qt_counter++;

					$qt_file_path = $qt_folder_name . '/' . $qt_file;
					
					?>

<!-- ======================================================================= -->

					<div class="qt-photo-gallery-item qt-image-no-<?php echo $qt_counter; ?>">
						<a class="qt-photo-gallery-item-link" href="<?php echo $qt_file_path; ?>" title="File name: <?php echo $qt_file; ?>">

							<div class="qt-photo-gallery-item-image-wrapper">

								<img src="<?php echo $qt_file_path; ?>" class="qt-photo-gallery-item-image" />

							</div>

						</a>
					</div>

<!-- ======================================================================= -->

					<?php

					if( $qt_counter == 5 ) {
						$qt_counter = 0;
					}

				endif;

			endwhile;

/* ========================================================================== */

			?>

		</section>

		<script src="_assets/js/jquery-3.2.1.min.js" type="text/javascript"></script>
		<script src="_assets/magnifix-popup/jquery.magnific-popup.min.js" type="text/javascript"></script>
		<script src="_assets/js/main.js" type="text/javascript"></script>

  </body>
</html>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.