Hi all,
I’m attemping to write my first PHP script to format and list the contents of a directory. Here’s what I have so far…
[CODE]
// …all HTML/CSS before this point…
<?php
$dir=”.”; // Directory where files are stored
if ( array($dir_list = opendir($dir)) && ($dir_list2 = opendir($dir)) ) {
$count = 0;
while ( (($filename = readdir($dir_list)) !== false) && (($prettyfilename = readdir($dir_list2)) !== false) ) {
$count = $count + 1;
switch ($prettyfilename) {
case (stristr($prettyfilename,”AD”)):
$prettyfilename = str_ireplace(“AD” , “Adelaide” , $prettyfilename);
break;
case (stristr($prettyfilename,”AK”)):
$prettyfilename = str_ireplace(“AK” , “Auckland” , $prettyfilename);
break;
case (stristr($prettyfilename,”BN”)):
$prettyfilename = str_ireplace(“BN” , “Brisbane” , $prettyfilename);
break;
case (stristr($prettyfilename,”BK”)):
$prettyfilename = str_ireplace(“BK” , “Burke Road” , $prettyfilename);
break;
case (stristr($prettyfilename,”CA”)):
$prettyfilename = str_ireplace(“CA” , “Canberra” , $prettyfilename);
break;
}
if( (is_int($count/2)) === TRUE){
echo “<tr>”;
echo “<td width=”200″ class=”oddrow”><a href=””.$filename.””>”.$prettyfilename.”</a></td>”;
echo “<td width=”200″ class=”oddrow”><i>[Earliest Archive]</i></td>”;
echo “<td width=”200″ class=”oddrow”><i>[Latest Archive]”.$count.”</i></td>”;
echo “</tr>”;
}else{
echo “<tr>”;
echo “<td width=”200″ class=”evenrow”><a href=””.$filename.””>”.$prettyfilename.”</a></td>”;
echo “<td width=”200″ class=”evenrow”><i>[Earliest Archive]</i></td>”;
echo “<td width=”200″ class=”evenrow”><i>[Latest Archive]”.$count.”</i></td>”;
echo “</tr>”;
}
}
closedir($dir_list);
closedir($dir_list2);
}
?>
// …all HTML/CSS after this point…
What I can’t figure out is how to sort this listing alphabetically. I have tried working with arrays but can never get it to work.
Can someone show me how to do this using the code above?
Many thanks!
Chris