ShibSta Posted May 21, 2006 Share Posted May 21, 2006 How can I get the following code to list things alaphabetically? I am fairly new to this so can you please post the script I need to change it to as I am more of a visual learner.Thanks[code] <?php echo $sys->imgdir; $sdir = '../'.$sys->imgdir; $dir = opendir($sdir.'/upload'); while(false !== ($file = readdir($dir))) { if($file != "." && $file != ".." && $file != "Thumbs.db") { if (file_exists($sdir.'/'.$file.'')) { rename($sdir.'/upload/'.$file.'', $sdir.'/upload/1-'.$file.''); echo '<option value="">REFRESH!</option>'; } else { echo '<option value="'.$file.'">'.$file.'</option>'; } } } closedir($dir); ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/ Share on other sites More sharing options...
jeremywesselman Posted May 21, 2006 Share Posted May 21, 2006 Here is a link to the php.net manual for readdir:[a href=\"http://www.php.net/manual/en/function.readdir.php\" target=\"_blank\"]http://www.php.net/manual/en/function.readdir.php[/a]There are a few examples in the user submits that alphabetize the directories.Personally, I thought that it would automatically alphabetize them based on the way it reads them.[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--] Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/#findComment-37596 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 basically you are gonna want to create an array of your files inside your while statement, sort the array, and then do another while (or foreach) loop to display them. Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/#findComment-37603 Share on other sites More sharing options...
ShibSta Posted May 21, 2006 Author Share Posted May 21, 2006 [!--quoteo(post=375682:date=May 21 2006, 03:34 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 03:34 AM) [snapback]375682[/snapback][/div][div class=\'quotemain\'][!--quotec--]basically you are gonna want to create an array of your files inside your while statement, sort the array, and then do another while (or foreach) loop to display them.[/quote]Thats pretty much how I thought I had to do it. However, when trying to actually do it, I got stuck.Could someone please post it for me?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/#findComment-37610 Share on other sites More sharing options...
jeremywesselman Posted May 21, 2006 Share Posted May 21, 2006 Post the code that you got stuck on and we'll see what we can do.[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--] Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/#findComment-37611 Share on other sites More sharing options...
.josh Posted May 21, 2006 Share Posted May 21, 2006 [code]<?php echo $sys->imgdir; $sdir = '../'.$sys->imgdir; $dir = opendir($sdir.'/upload'); while(false !== ($file = readdir($dir))) { $files[] = $file; } sort ($files); $x = 0; while ($files[$x]) { if($files[$x] != "." && $files[$x] != ".." && $files[$x] != "Thumbs.db") { if (file_exists($sdir.'/'.$files[$x].'')) { rename($sdir.'/upload/'.$files[$x].'', $sdir.'/upload/1-'.$files[$x].''); echo '<option value="">REFRESH!</option>'; } else { echo '<option value="'.$files[$x].'">'.$files[$x].'</option>'; } } $x++; } closedir($dir);?>[/code]edited to reformat it prettier. Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/#findComment-37615 Share on other sites More sharing options...
ShibSta Posted May 22, 2006 Author Share Posted May 22, 2006 [!--quoteo(post=375694:date=May 21 2006, 04:25 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 21 2006, 04:25 AM) [snapback]375694[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]<?php echo $sys->imgdir; $sdir = '../'.$sys->imgdir; $dir = opendir($sdir.'/upload'); while(false !== ($file = readdir($dir))) { $files[] = $file; } sort ($files); $x = 0; while ($files[$x]) { if($files[$x] != "." && $files[$x] != ".." && $files[$x] != "Thumbs.db") { if (file_exists($sdir.'/'.$files[$x].'')) { rename($sdir.'/upload/'.$files[$x].'', $sdir.'/upload/1-'.$files[$x].''); echo '<option value="">REFRESH!</option>'; } else { echo '<option value="'.$files[$x].'">'.$files[$x].'</option>'; } } $x++; } closedir($dir);?>[/code]edited to reformat it prettier.[/quote]Was that supposed to be a working code aswell that sort's them? If so, well, it don't work. It's still not listed alphabetically. :( Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/#findComment-37955 Share on other sites More sharing options...
.josh Posted May 22, 2006 Share Posted May 22, 2006 really? because it seems to be working fine for me... maybe you don't understand [b][i]how[/i][/b] the sort works. the order in which things are sorted is this (space being first): !"#$%&'()*+,-.0123456789<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\^_`abcdefghijklmnopqrstuvwxyz|~Since capitalized letters take precidence over lowercase, it will first alphabetize by capital letters, then lowercase letters, so if you had a list like this:appleAdamJollySilkannoyboredblahHeadacheit would sort like this:AdamHeadacheJollySilkannoyappleblahbored Quote Link to comment https://forums.phpfreaks.com/topic/10100-list-options-alphabetically/#findComment-38093 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.