Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.