Jump to content

List Options Alphabetically


ShibSta

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]
Link to comment
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--]
Link to comment
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
Link to comment
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.
Link to comment
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. :(
Link to comment
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.