goatboy Posted December 4, 2007 Share Posted December 4, 2007 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> Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 4, 2007 Share Posted December 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
goatboy Posted December 4, 2007 Author Share Posted December 4, 2007 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>"; } Quote Link to comment Share on other sites More sharing options...
goatboy Posted December 4, 2007 Author Share Posted December 4, 2007 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(); ?> 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.