I collect the files from a folder using some Smarty code:
{assign var=files value="uploads/Documenten/GPS-Tracks/*.html"|glob}
{if count($files)}
<ul>
{foreach from=$files item='file'}
{assign var='gpstrack_location' value="{$file|cms_escape:'urlpathinfo'}"}
{if !$file|contains:"index"}
<li><a href="{$file}">{$file|basename|substr:0:-5}</a></li>
{/if}
{/foreach}
</ul>
{/if}
This gives me a sorted list basedon the first character.
What I'd like to achive is a sorted list based on the last two charaters. Is there in Smarty or PHP a way to do this?
Thanks in advance for your help!
UPDATE: I got myself a couple of steps futher :) There remains one warning I'm not able to solve: The code so far:
<?php
$folder = '<some foldername>';
$files = array_values(preg_grep('/^([^.])/', scandir($folder, SCANDIR_SORT_ASCENDING)));
//$files = scandir($folder);
//print_r($files);
usort(var_dump($files, function ($a, $b){
if ($a || $b <> "")
return substr($b, strlen($b) - 2) - substr($a, strlen($a) - 2);
}));
?>
The warning I get, and thought to solve with the 'or'-statement:
[
Someone able to get me a step further?
Found a solution.
<?php
$files = glob($folder.$file_name);
$current_year = date('y');
if (
usort($files, function($a, $b) {
$yearA = (int) substr($a, -7); // extension + . + last two characters
$yearB = (int) substr($b, -7);
return $yearB - $yearA; // descending, for ascending swap $yearA and $yearB
})
) {
// $files is now sorted, do your stuff here
foreach ((array) $files as $file) {
if (substr(basename($file, '.html'), -2) == $current_year) {
echo basename($file, '.html');
echo "<br />";
} else {
$current_year = $current_year - 1;
}
}
} else {
// sorting failed, show error message, exception, etc.
echo "Files not sorted";
}
?>