Jump to content

Sorting dropdown list


Ty44ler

Recommended Posts

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);

 

Link to comment
https://forums.phpfreaks.com/topic/215218-sorting-dropdown-list/
Share on other sites

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.