I have a ton of files that are all audio and all named with a date in this format:
1999-12-18_song_name.mp3
I want to list the dir contents dynamically and put a line break each time the year changes. I decided to extract the year by taking the 1st four digits of the file, and if they don't equal the first four digits of the file before it, put a line break. But it inserts line breaks apparently randomly. Some are in the wrong place, some don't have one where it should. This script outputs the previous year and the current year properly, but the if condition isn't working. Can someone tell me what's wrong?
<?
//everything works up til here...
$sortedarray = $songarray;
sort($sortedarray);
//foreach loop writes the code
foreach ( $sortedarray as $title ) {
//finds song size
$songsize = roundit("$title");
//links to the download wrapper
echo "<td width=60%><center><a href=\"./download.php?song=$title\">\n";
//sets previous showyear
if (isset($showyear)) { $prevshowyear = $showyear; }
//extracts year (there might be a better way, but this does work
$showy = $title{0};
$showye = $showy.$title{1};
$showyea = $showye.$title{2};
$showyear = $showyea.$title{3};
//cleans up the file name for display
$filename = (str_replace("_"," ",$title));
$filename = (str_replace(".mp3","",$filename));
//note that $showyear and $prevshowyear are correct here!
echo $filename . " </a> (year: $showyear, $prevshowyear) </td>";
echo "<td width=40%><center>" . $songsize;
echo " MB</td></tr>";
//here's the problem...
if ($prevshowyear != $showyear) { echo "<tr><td colspan=2> </td></tr>\n"; }
else { echo "\n"; }
end foreach loop
}
?>
I also changed the last lines to if prevshowyear = showyear and prevshowyear==showyear (with reversed conditions) and it didn't work either. What could be wrong?
Thanks in advance ya'll,
-Adam