Ty44ler Posted October 5, 2010 Share Posted October 5, 2010 I'm trying to sort this dropdown box. It reads from a directory, and lists the file name in the dropdown box. Here's the tricky part... the filename is listed differently in the dropdown than in the directory by using explode(). I want to sort it though since it's still being sorted by the directory listings... For example: Filename starts out as: 123_abc_567.pdf then gets listed as abc_123_567.pdf in the dropdown, but it's still getting sorted as if it were 123_abc_567.pdf How can I do that? Here's my code: // Define the full path to folder from root $path = "C:/Work_Orders/"; // Open the folder $dir_handle = @opendir($path) or die("Unable to open $path"); echo "<form method=\"POST\" action='".$_SERVER['PHP_SELF']."' name='selectworkorder'><select name='ordernumber2'>"; // Loop through the files while ($file = readdir($dir_handle)) { //Remove file extension $ext = strrchr($file, '.'); if($ext !== false) { $file = substr($file, 0, -strlen($ext)); } if($file == "." || $file == ".." || $file == "index.php" ) continue;//explode file name $changedordernumber = explode("_",$file);//put in new order$changedordernumber = $changedordernumber[1]."_".$changedordernumber[0]."_".$changedordernumber[2]; $changedordernumber=trim($changedordernumber,"_"); //list optionsecho "<option name='$file' value='$file'>$changedordernumber</option>\n"; } echo "</select><input type='submit' value='Change' name='submit'/></form></div>"; // Close closedir($dir_handle); Quote Link to comment Share on other sites More sharing options...
jammesz Posted October 6, 2010 Share Posted October 6, 2010 Try this: $final_results=array(); $i=0; while ($file = readdir($dir_handle)) { //Remove file extension $ext = strrchr($file, '.'); if($ext !== false) { $file = substr($file, 0, -strlen($ext)); } if($file == "." || $file == ".." || $file == "index.php" ) continue; //explode file name $changedordernumber = explode("_",$file); //put in new order $changedordernumber = $changedordernumber[1]."_".$changedordernumber[0]."_".$changedordernumber[2]; $changedordernumber=trim($changedordernumber,"_"); $i++; $final_results[$changedordernumber.$i]=array($file,$changedordernumber); } ksort($final_results); foreach($final_results as $FR){ //list options echo "<option name='$FR[0]' value='$FR[0]'>$FR[1]</option>\n"; } Quote Link to comment Share on other sites More sharing options...
Ty44ler Posted October 7, 2010 Author Share Posted October 7, 2010 That worked! Thank you so much! Quote Link to comment Share on other sites More sharing options...
jammesz Posted October 7, 2010 Share Posted October 7, 2010 Glad to help 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.