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> Link to comment https://forums.phpfreaks.com/topic/80171-scan-directly-put-folder-in-select-box/ 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 Link to comment https://forums.phpfreaks.com/topic/80171-scan-directly-put-folder-in-select-box/#findComment-406346 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>"; } Link to comment https://forums.phpfreaks.com/topic/80171-scan-directly-put-folder-in-select-box/#findComment-406368 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(); ?> Link to comment https://forums.phpfreaks.com/topic/80171-scan-directly-put-folder-in-select-box/#findComment-406389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.