dilshan Posted November 11, 2014 Share Posted November 11, 2014 Hi, i have an issue to sort gallery photos by date. Right now its sort by Value (Name). here is the code index.php contains : <div> <?php foreach($categories_array as $photo_category=>$photos_array){?> <?php $category_thumbnail = $gallery_url."/layout/pixel.gif"; if(file_exists('files/'.$photo_category.'/thumbnail.jpg')){ $category_thumbnail = $gallery_url.'/'.$photo_category.'/thumbnail.jpg'; } $category_url = $gallery_url.'/'.$photo_category; ?> <span class="category_thumbnail_span" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height+20;?>px;"> <a class="category_thumbnail_image" href="<?php echo $category_url;?>" style="width:<?php echo $settings_thumbnail_width;?>px; height:<?php echo $settings_thumbnail_height;?>px; background-image:url('<?php echo $gallery_url;?>/layout/lens_48x48.png');" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> <img src="<?php echo $category_thumbnail;?>" width="<?php echo $settings_thumbnail_width;?>" height="<?php echo $settings_thumbnail_height;?>" alt="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>" /> </a> <a class="category_thumbnail_title" href="<?php echo $category_url;?>" title="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)));?>"> <?php echo htmlentities(str_replace('-',' ', truncate_by_letters($photo_category, 16, '..')), ENT_QUOTES, "UTF-8");?> (<?php echo count($photos_array);?>) </a> </span> <?php } ?> </div> the sort function : ksort($categories_array); Quote Link to comment https://forums.phpfreaks.com/topic/292404-php-sort-array-by-date/ Share on other sites More sharing options...
Ch0cu3r Posted November 11, 2014 Share Posted November 11, 2014 What is the array structure for $categories_array? You may need to use array_multisort instead of ksort Quote Link to comment https://forums.phpfreaks.com/topic/292404-php-sort-array-by-date/#findComment-1496311 Share on other sites More sharing options...
dilshan Posted November 11, 2014 Author Share Posted November 11, 2014 (edited) $categories_array = array(); and // loop over files directory and read the categories if ($handle = opendir('files')) { while (false !== ($folder = readdir($handle))) { if(is_dir('files/'.$folder) and $folder!='.' and $folder!='..'){ // define this key in the array, it will be blank, store categories as keys $categories_array[$folder] = array(); // $total_photos_array[$folder] = 0; $files_in_dir = scandir('files/'.$folder); foreach($files_in_dir as $file){ // if file ends in _thumb.jpg if(strpos($file, '_thumb.jpg') === strlen($file)-10){ // $total_photos_array[$folder]++; $base_file_name = substr($file, 0, strlen($file)-10); // insert this file in the array of files array_push($categories_array[$folder], $base_file_name); } } } } closedir($handle); } Edited November 11, 2014 by dilshan Quote Link to comment https://forums.phpfreaks.com/topic/292404-php-sort-array-by-date/#findComment-1496312 Share on other sites More sharing options...
Ch0cu3r Posted November 11, 2014 Share Posted November 11, 2014 Wheres the date? I guess you mean to say you want to sort the files in the order they where created? Quote Link to comment https://forums.phpfreaks.com/topic/292404-php-sort-array-by-date/#findComment-1496346 Share on other sites More sharing options...
dilshan Posted November 12, 2014 Author Share Posted November 12, 2014 Wheres the date? I guess you mean to say you want to sort the files in the order they where created? Yes Quote Link to comment https://forums.phpfreaks.com/topic/292404-php-sort-array-by-date/#findComment-1496381 Share on other sites More sharing options...
dilshan Posted November 12, 2014 Author Share Posted November 12, 2014 I think i should fit this code somewhere $files = array(); if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $files[filemtime($file)] = $file; } } closedir($handle); // sort ksort($files); // find the last modification $reallyLastModified = end($files); foreach($files as $file) { $lastModified = date('F d Y, H:i:s',filemtime($file)); if(strlen($file)-strpos($file,".swf")== 4){ if ($file == $reallyLastModified) { // do stuff for the real last modified file } echo "$file$lastModified"; } } Quote Link to comment https://forums.phpfreaks.com/topic/292404-php-sort-array-by-date/#findComment-1496382 Share on other sites More sharing options...
dilshan Posted November 13, 2014 Author Share Posted November 13, 2014 hi, any update ? Quote Link to comment https://forums.phpfreaks.com/topic/292404-php-sort-array-by-date/#findComment-1496467 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.