darkapec Posted April 15, 2011 Share Posted April 15, 2011 I am a php newb. I have 2 somewhat simple problems. First I have a huge list of movies that I would like sorted in a drop down list alphabetically. I already have the drop down list made and the movies show up correctly. But I cannot figure out how to sort the list. Here is the code <?php $dir="/backup/Movies"; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.php") { $thelist .= '<option>'.$file.'</option>'; } } closedir($handle); } ?> <form action="listen.php" method="post" name="table"> <select name="Movie"> <P><?=$thelist?></p>' </select> </form> Second problem is inside this directory there are a few other directories is it possible to make the directories show at the top instead of them being sorted alphabetically with the movies. Thanks in advance Jake Quote Link to comment https://forums.phpfreaks.com/topic/233777-sort-alphabetically-directories/ Share on other sites More sharing options...
kenrbnsn Posted April 15, 2011 Share Posted April 15, 2011 You can either use the glob function which returns the files already sorted, or put the files into an array and sort that array before generating the HTML. Something like: <?php $dir="/backup/Movies"; $files = array(); if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.php") { $files[] = $file; } } closedir($handle); } sort($files) ?> <form action="listen.php" method="post" name="table"> <select name="Movie"> <p> <?php $tmp = array(); foreach ($files as $file) { $tmp[] = "<option value='$file'>$file</option>"; } echo implode("\n",$tmp) . "\n"; ?> </p> </select> </form> As for your other question, use 2 arrays and put the files in one array and the directories in the other array. Sort each array and then output the contents. Ken Quote Link to comment https://forums.phpfreaks.com/topic/233777-sort-alphabetically-directories/#findComment-1201881 Share on other sites More sharing options...
darkapec Posted April 15, 2011 Author Share Posted April 15, 2011 Thank you much for your help, as for using multiple scripts for each folder... that is exactly what I was thinking, after I posted of course. Thanks again for your fast response time. I found a lot of info on sorting alphabetically on the forums but they all seemed so code specific and because I am so new to .php I really needed the help. Jake Quote Link to comment https://forums.phpfreaks.com/topic/233777-sort-alphabetically-directories/#findComment-1201889 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.