Jump to content

Scan directly put folder in select box


goatboy

Recommended Posts

I was able to figure out this code to scan a directory and put all of the folders in a select box

the problem is i can't get them in alphabetical order, can anyone help with this?

 

 

<form method="get">
<select onChange="top.location.href=this.options[this.selectedIndex].value">
<option>--Bands--</option>
<?


   $mydir = dir('./path_to_directory/'); //include the trailing slash here
  
while(($file = $mydir->read()) !== false) {

$upper = ucwords($file);

     if(is_dir($mydir->path.$file)  == true && $file != '.' && $file != '..') {

	 echo "<option value=\"url_goes_here\">$variable</option>";

      }

   }
   $mydir->close();
?>
</select>
<input type="submit" value="GO" />

</form>

Link to comment
https://forums.phpfreaks.com/topic/80171-scan-directly-put-folder-in-select-box/
Share on other sites

Instead of echoing out directly to your drop-down, store the data into an array

 

<?php
     if(is_dir($mydir->path.$file)  == true && $file != '.' && $file != '..') {

	$dir_data[] = $variable;

      }

 

Then you can sort the array (the folders) any way you desire before populating your drop-down with a FOR loop based on count($dir_data).

 

PhREEEk

here's what i did but it did'nt work

 

     if(is_dir($mydir->path.$file)  == true && $file != '.' && $file != '..') {

 $dir_data[] = $variable;

$dir_data = sort($dir_data);

 foreach($dir_data as $folder){

 echo "<option>$folder</option>";



      }

sorry, i had a parse error, so it's partly working but the results are looping, it list the results 3 times

 

<?


   $mydir = dir('./templates/'); //include the trailing slash here
  
while(($file = $mydir->read()) !== false) {

$upper = ucwords($file);


     if(is_dir($mydir->path.$file)  == true && $file != '.' && $file != '..') {

	 $data_dir[] = $upper;

	 foreach ($data_dir as $value) {
    echo "$value <br />\n";
}

      }

   }
   $mydir->close();
?>

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.