jerel Posted July 2, 2007 Share Posted July 2, 2007 Hello, I have the code below, looking at the contents of the folder and listing the files, as links to themselves. I would like to be able to control the way they are sorted. It seems very random. I figured i could write all the file names to an array then have them write to a db, then pull those back from the db. So basically to avoid the extra scripting, is there some way to sort this? thanks ??? code below $dir = "../archives"; $dh = opendir($dir) or die ("could not open dir"); while ( !(($file = readdir($dh)) === false) ) { if (($file != ".") && ($file != "..")) { //echo the link to the file here using the $file variable echo "<a href='http://www.amimpact.com/archives/$file'>$file</a><br />"; } } Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 2, 2007 Share Posted July 2, 2007 read into array sort array echo array elements with links http://ca.php.net/manual/en/function.natsort.php Quote Link to comment Share on other sites More sharing options...
jerel Posted July 3, 2007 Author Share Posted July 3, 2007 Hello, yes, thanks i also found the array_reverse() very helpful, here is the code if someone else would like to see this finished. $dir = "../archives"; $dh = opendir($dir) or die ("could not open dir"); $n = "0"; while ( !(($file = readdir($dh)) === false) ) { if (($file != ".") && ($file != "..")) { $n = $n+1; //echo the link to the file here using the $file variable $movies[$n] = "$file"; } } natsort($movies); $newray = array_reverse($movies); $array_count = count($newray); for($i = 0; $i < $array_count; $i++) { // echo whatever you want using $newray[$i] as your resorted array echo "$newray[$i]"; //phpfreaks.com ROCKS THE HOUSE } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.