Kimzerx Posted June 18, 2018 Share Posted June 18, 2018 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; } Quote Link to comment Share on other sites More sharing options...
Barand Posted June 18, 2018 Share Posted June 18, 2018 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. Quote Link to comment Share on other sites More sharing options...
Kimzerx Posted June 18, 2018 Author Share Posted June 18, 2018 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> Quote Link to comment Share on other sites More sharing options...
Barand Posted June 18, 2018 Share Posted June 18, 2018 No. use rsort($files) as I said, instead of array_multisort(). Quote Link to comment Share on other sites More sharing options...
Barand Posted June 18, 2018 Share Posted June 18, 2018 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); } 1 Quote Link to comment Share on other sites More sharing options...
Kimzerx Posted June 24, 2018 Author Share Posted June 24, 2018 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> Quote Link to comment Share on other sites More sharing options...
requinix Posted June 24, 2018 Share Posted June 24, 2018 That's a nice get_files() function you have there. Shame you aren't using... Quote Link to comment Share on other sites More sharing options...
Kimzerx Posted June 24, 2018 Author Share Posted June 24, 2018 Im very very very new to this. So im trying to understand, you say im not using it. So there must be somewhere i should call the function but im not understanding where that is. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2018 Share Posted June 24, 2018 You call it when you need the results (ie the list of files) $qt_folder_name = 'images'; foreach (get_files($qt_folder_name) as $imagename) { echo "<img src='$qt_folder_name/$imagename'><br>"; } 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.