Jump to content


Photo

List Options Alphabetically


  • Please log in to reply
7 replies to this topic

#1 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 21 May 2006 - 04:50 AM

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

                <?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);
                ?>


#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 21 May 2006 - 06:50 AM

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--]

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 07:34 AM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 21 May 2006 - 08:04 AM

[!--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) View Post[/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

#5 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 21 May 2006 - 08:10 AM

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--]

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 21 May 2006 - 08:25 AM

<?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);
?>

edited to reformat it prettier.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 ShibSta

ShibSta
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 22 May 2006 - 02:43 PM

[!--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) View Post[/div][div class=\'quotemain\'][!--quotec--]
<?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);
?>

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. :(

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 May 2006 - 11:22 PM

really? because it seems to be working fine for me...

maybe you don't understand how 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


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users