SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: sort problem

  1. #1
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sort problem

    I'm having a hell of a time sorting this alphabetically. Could somebody help?

    PHP Code:
    $currentdir SITE_PATH.$directory;
    $new_file null;
    @
    $dir opendir($currentdir);
    while(
    $file readdir($dir)) {
        if(!
    is_dir($currentdir."/".$file)) {
            
    $file $file "','";
            
    $new_file .= $file;
        }


  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you just trying to sort the files in a directory alphabetically?

    Stick them inside an array, then use asort()

    PHP Code:
    @$dir opendir($currentdir);
    while(
    $file readdir($dir))
        if(!
    is_dir($currentdir."/".$file))
             
    $files[] = $file;
        
    asort($files); 

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of a flat file, any thoughts of creating an array of arrays ? You can then iterate through and use asort().
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  4. #4
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you.

    The problem that I have is that the $new_file var is put into a pulldown, which then displays filenames of images in the dir.

    I can't really toy with the code, and what you gave me doesn't work too well. Meaning, instead of each image being an <option>, all of the images in the dir showed up as one <option>. Is there a way to sort with the code that I supplied?

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    Toronto
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to get all the files inside $new_file spaced with a comma, just use implode


    PHP Code:
    $new_file implode(","$files);
    //now $new_file = 'file1,file2,file3...' 
    or to get each inside an option, loop through the array
    PHP Code:
    foreach($files as $file)
      
    $dropdown .= "<option>$file</option>"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •