How can i sort by date?

I managed to sort photos by date. The pictures in the folder, but when I click on the thumb image does not appear. help me :frowning:

$categories_array = array();

    $images = 'files/';
    if ($handle = opendir($images)) 
    {
        while (false !== ($folder = readdir($handle))) 
        {
            if(is_dir($images.$folder) and $folder!='.' and $folder!='..')
            {
                $categories_array[$folder] = array();
                $files_in_dir = scandir($images.$folder);
                $categories_array[$folder] = filemtime($images.$folder);
                foreach($files_in_dir as $file)
                {
                    if(strpos($file, '_thumb.jpg') === strlen($file)-10)
                    {


                        $base_file_name = substr($file, 0,  strlen($file)-10);
                        array_push($categories_array[$folder],$base_file_name);
                    }
                }

            }
        }
        closedir($handle);
    }


    arsort($categories_array);

You need to show us the code that outputs the link that you’re having problems clicking on. If you view the HTML that is output, does the filename in the link correspond to the name you’re expecting to see? I imagine you’re outputting the $base_file_name that you create if the filename ends in _thumb.jpg, but can’t look for typos or the like.

html code:

<?php if(count($categories_array)<=0){?>
<p>There are no photo categories, create one or more categories before uploading photos</p>
<?php } ?>
<?php if(count($categories_array)>0){?>
	<?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.'/'.rawurlencode($photo_category).'.jpg';
		}
		$category_url = $gallery_url.'/'.rawurlencode($photo_category);
		?>
<div class="col-sm-6 col-md-3">        
<div class="news"><img class="news-image" src="<?php echo $category_thumbnail;?>" alt="<?php echo htmlentities(ucwords(str_replace('-', ' ', $photo_category)), ENT_QUOTES, "UTF-8");?>" />
<div class="news-caption"><a  class="btn btn-transparent" href="<?php echo $category_url;?>" ><?php echo htmlentities(str_replace('-',' ', truncate_by_letters($photo_category, 26, '..')), ENT_QUOTES, "UTF-8");?>  
        </a>
</div>        
</div>  
</div>

OK, can you post a var_dump of the categories_array? And the source of the html page from your browser when it’s displayed the page? I’m a bit confused as to how the array is laid out, and it also appears that it will only add the picture to the array if the last 10 characters of the name are ‘_thumb.jpg’, and then it will strip that part before adding it.

please download my zip file: http://www.lunaicmimarlik.com/free_php_gallery.zip
working on system_header.php thank you.

Sorry, I won’t download random zip files and open them. Besides, you need to post the output of your second bit of code after it’s run on your system with the various image files in the directory, just seeing more source isn’t going to help and I can’t really spend the time setting it all up on my local system. Maybe someone with more experience will be able to offer some advice.

OK, thanks for your interest.

RESOLVED:

function dbg($data) { 
if (is_array ( $data )) 
$output = "<script>console.log( 'Debug Objects: " . implode ( ',', $data ) . "' );</script>"; 
else 
$output = "<script>console.log( 'Debug Objects: " . $data . "' );</script>"; 
echo $output; 
} 
$categories_array = array (); 
$temp_array = array (); 
$timer_1 = microtime ( true ); 

$scandir_array = scandir ( 'files' ); 
foreach ( $scandir_array as $folder ) { 
if (is_dir ( 'files/' . $folder ) and $folder != '.' and $folder != '..') { 
$timestamp = filemtime ( 'files/' . $folder ); 
$temp_array[$timestamp] = $folder; 
} 
} 
// dbg($temp_array[1424218218]); 

krsort ( $temp_array ); // sorts an array by key. 
dbg($temp_array); 

foreach ( $temp_array as $folder ) { 
// define this key in the array, it will be blank, store categories as keys 
$categories_array [$folder] = array (); 
$files_in_dir = scandir ( 'files/' . $folder ); 
foreach ( $files_in_dir as $file ) { 
if ($file != '.' and $file != '..') { 
// if file is not the category thumbnail (thumbnail.jpg) and not _thumb.jpg and not _small.jpg 
if ($file != "thumbnail.jpg" and substr ( $file, strlen ( $file ) - 10 ) != "_small.jpg" and substr ( $file, strlen ( $file ) - 10 ) != "_thumb.jpg") { 
$base_file_name = substr ( $file, 0, strlen ( $file ) - 4 ); 
// insert this file in the array of files 
array_push ( $categories_array [$folder], $base_file_name ); 
} // if 
} // if 
} // foreach 
} // foreach

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.