The script below lists out all my mp3's in a folder and echo's it into a html table so I can sort after the title headers.
Problem is that I want the default sorting to be alphabetically after filename. But when I try to change the order of the sorting below :
if ($sort=='name') { usort($dir, "sort_name"); }
elseif ($sort=='size') { usort($dir, "sort_size"); }
else { usort($dir, "sort_time"); }
to
if ($sort=='size') { usort($dir, "sort_size"); }
elseif ($sort=='time') { usort($dir, "sort_time"); }
else { usort($dir, "sort_name"); }
the date sorting doesnt work properly anymore.
What am I doing wrong ?
Below is the full code :
<style type="text/css">
* {
font-family: Helvetica;
}
a:link { text-decoration: none; }
table.b {
border-radius: 0px 20px 20px 0px;
background: linear-gradient(to right,white,lightblue,white);
padding: 3px;
box-shadow: gray 5px -5px 15px;
border: dotted 2px;
}
td.c {
border-radius: 0px 20px 20px 0px;
background: linear-gradient(to right,lightblue,white,lightblue);
}
.glow {
text-decoration: underline;
color: white;
font-weight: bold;
text-shadow: 0px 0px 5px lime, 0px 0px 10px orange;
}
</style>
<?php
// Variables
$dir = glob("*.mp3");
$sort = $_GET['sort'];
// Functions for sorting each table column title
function sort_name($a,$b) { return $a > $b; }
function sort_time($a,$b) { return filemtime($b) - filemtime($a); }
function sort_size($a,$b) { return filesize($a) < filesize($b); }
// Checks wich table column title is selected
if ($sort=='size') { usort($dir, "sort_size"); }
elseif ($sort=='time') { usort($dir, "sort_time"); }
else { usort($dir, "sort_name"); }
// Table
echo "<table class=b>";
echo "<tr>";
$titles = array('nr','name','size','date');
foreach($titles as $title) {
if ($title==$sort) { echo "<td> <a href=$_SERVER[PHP_SELF]?sort=$title>".ucfirst($title)."</a> </td>"; }
elseif ($title=='nr') { echo "<td class=glow><a href=./> <span class=glow>".ucfirst($title)."</span></a> </td>"; }
else { echo "<td> <a href=$_SERVER[PHP_SELF]?sort=$title>".ucfirst($title)."</a> </td>"; }
}
echo "</tr>";
// Table content
for($i=0; $i<count($dir); $i++) {
echo "<tr>";
echo "<td> " . $i . " </td>";
echo "<td> <a href=" . rawurldecode($dir[$i]) . ">" . $dir[$i] . "</a> </td>";
echo "<td> " . round(filesize($dir[$i])/(1024)/(1024),1) . " MB </td>";
echo "<td> " . date("d.m.Y", filemtime($dir[$i])) . "<sub> - " .date("H:i:s", filemtime($dir[$i])) . "</sub> </td>";
echo "</tr>";
}
echo "</table>";
?>