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 />"; } } Link to comment https://forums.phpfreaks.com/topic/58127-solved-putting-a-file-directory-in-a-specific-order/ 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 Link to comment https://forums.phpfreaks.com/topic/58127-solved-putting-a-file-directory-in-a-specific-order/#findComment-288290 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 } ?> Link to comment https://forums.phpfreaks.com/topic/58127-solved-putting-a-file-directory-in-a-specific-order/#findComment-289147 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.