Jump to content

Archived

This topic is now archived and is closed to further replies.

ShibSta

List Options Alphabetically

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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--]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[!--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

Share this post


Link to post
Share on other sites
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--]

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
[!--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. :(

Share this post


Link to post
Share on other sites
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:

apple
Adam
Jolly
Silk
annoy
bored
blah
Headache

it would sort like this:

Adam
Headache
Jolly
Silk
annoy
apple
blah
bored

Share this post


Link to post
Share on other sites

×

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.