Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 31
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation HELP! need someone brainy.

    How can i change this code to make the files show up, A to Z.

    PHP Code:
    $path "digits";
    $dh opendir($path);
    while (
    $file readdir($dh)) {
        if (
    is_dir($path."/".$file)) {
            if (!
    is_file($path."/".$file)) {
                if ((
    $file!=".") && ($file!="..")) {
                    echo 
    "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";
                }
            }
        }
    }
    closedir($dh); 

    Any suggestions are greatful.
    Last edited by cragllo; 04-27-2004 at 07:20 PM.

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    im assuming you mean so that they show up alphabetically?

    well what I would do, is store the values in an array and then sort them.... like so:

    PHP Code:
    $files = array();

    $path 'digits';
    $dh opendir($path);
    while (
    $file readdir($dh)) {
        if (
    is_dir($path.'/'.$file)) {
            if (!
    is_file($path.'/'.$file)) {
                if ((
    $file!='.') && ($file!='..')) {
                    
    $files[] = $file;
                }
            }
        }
    }
    closedir($dh);

    sort($files);

    for(
    $i=0;$i<count($files);$i++){

         echo 
    "<p><table border=0><tr><td class=\"boldtitle\">".$files[$i]."</td></tr><tr><td><img src=\"digits/".$files[$i].".gif\"></td></tr></table><br></p>\n"
     


    You may want to take a look at http://www.php.net/sort to see how this works...

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
     $files = Array();
     
    $path "digits"
     
    $dh opendir($path); 
     while (
    $file readdir($dh)) { 
      if (
    is_dir($path."/".$file)) { 
       if (!
    is_file($path."/".$file)) { 
        if ((
    $file!=".") && ($file!="..")) { 
         
    $files[] = $file;
        } 
       } 
      } 
     } 
     
    closedir($dh);  

     
    asort($files);
     foreach(
    $files as $index=>$file) {
      echo 
    "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n"
     } 
    That should do the trick.. I dunno if there's a better way..

    Edit: Whoops! Almost at the same time, eh missing-score? On the same track though


    Sadiq.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to note in the future, that you should give a more descriptive topic title, a good title for this post would be "Ordering files in a directory" or "Alphabetical Directory Listing".

    http://www.codingforums.com/postguide.htm

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    looks like mine and sad69's posts were crossed, although it seems we had pretty much the same idea... The only difference is I used sort and he used asort().

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Thank you guys, I'll try them both to see which one is best, I have only started learning PHP a few days ago, so I dont know much yet, I'm glad i found this forum,
    Thanx again.

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    They will do exactly the same thing for what you want... They way we loop through each file is slightly different so its really up to your prefrence

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    The second code worked, the first one did not. wierd, dunno y.

    Im glad I found it tho. Its good how you can just add files and it will find them for you, amazing if you ask me. Thanks guys! Its for a hit counter service im doing.

    have a look at what I was trying to do if you want:
    http://www.hostultra.com/~sponkindus...x.php?p=styles

  • #9
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oops , simple typing error... glad you got it working anyway

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Ok guys, thank you for helping. But... I need to change the code again, I need it so it only shows the image files without searching the folders. Also to still go A-Z,
    Anyone?

  • #11
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm.. I don't know too much about this stuff, so I dunno if you can just ask if a file is an image file...

    You may need to clarify what you consider to be an image file (by extension or something..). Once you've got that figured out, insert those statements where I've got the red colour..

    PHP Code:
     $files = Array(); 
     
    $path "digits";  
     
    $dh opendir($path);  
     while (
    $file readdir($dh)) {  
      if (
    is_dir($path."/".$file)) {  
       if (!
    is_file($path."/".$file)) {  
        if ((
    $file!=".") && ($file!="..") [COLOR=Red]&& (preg_match(/gif/i,$file) && (preg_match(/jpg/i,$file)[/COLOR]) {  
         
    $files[] = $file
        }  
       }  
      }  
     }  
     
    closedir($dh);   

     
    asort($files); 
     foreach(
    $files as $index=>$file) { 
      echo 
    "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n";  
     } 
    Like I say, I don't know too much about this kind of stuff, so there may be a better way...

    Hope that helps,
    Sadiq.

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    It didnt work, It's stull searching for the folder, and the image wont show up if the folder aint there. Thanx neway

  • #13
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:

    PHP Code:
     $files = Array();
     
    $path "digits"
     
    $dh opendir($path); 
     while (
    $file readdir($dh)) { 
      if (
    is_dir($path."/".$file)) { 
       if (!
    is_file($path."/".$file)) { 
        if ((
    $file!=".") && ($file!="..") && preg_match("/\.(gif|jpg|jpeg|bmp)/is"$file)) { 
         
    $files[] = $file;
        } 
       } 
      } 
     } 
     
    closedir($dh);  

     
    asort($files);
     foreach(
    $files as $index=>$file) {
      echo 
    "<p><table border=0><tr><td class=\"boldtitle\">".$file."</td></tr><tr><td><img src=\"digits/".$file.".gif\"></td></tr></table><br></p>\n"
     } 

  • #14
    Regular Coder
    Join Date
    Apr 2004
    Posts
    164
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    that did not work either, would it be easier to do a new code? i think its because on the code, it's on a diffrent folder that the last time.

  • #15
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    shouldnt make any difference, i think this may work:

    Just replace this line in the code:
    PHP Code:
    if (($file!=".") && ($file!="..") && preg_match("/(\.gif|\.jpg|\.jpeg|\.bmp+)/is"$file)) { 


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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