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.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Group files by first letter

    I have a small piece of code that displays all the files in a directory. What I am trying to do is get the files to group by the first letter of each file.

    So it would look like this:

    A
    Aword
    Aword
    awrd

    B
    bword
    Bword

    etc....

    Here is my code so far.

    PHP Code:
    <?php
    $path 
    ".";
    $narray=array();
    $dir_handle = @opendir($path) or die("Unable to open $path");
    $i=0;
    while(
    $file readdir($dir_handle))
    {
    if (
    $file != '.' && $file != '..' && $file != 'index2.php' && $file != 'index.php')
    {
    //$name = pathinfo($file);
    //echo '<a href="/forms/files/'.$file.'">'.$name['filename'].'</a><br/>';
    $narray[$i]=$file;
    $i++;
    }
    }
    usort($narray'strcasecmp');

    for(
    $i=0$i<sizeof($narray); $i++)
    {
    $name pathinfo($narray[$i]);
    $thelist .= $narray[$i]. '<br/><br/>';
    echo 
    '<a href="'.$path.$narray[$i].'">'.$name['filename'].'</a><br />';}

    //closing the directory
    closedir($dir_handle);
    //$dir = getcwd();
    $bas basename($dir);
    ?>
    Any help would be greatly appreciated.
    Last edited by xhris; 01-17-2012 at 10:05 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Okay, you've already done the best thing by creating an array for this. Now there are several options.
    Following what you have here, you track the first char last known, and use ul's to deal with them.
    PHP Code:
    $cLast null;
    $iTotal count($narray);
    for (
    $i 0$i $iTotal; ++$i)
    {
        if (
    $narray[$i][0] != $cLast)
        {
            if (
    $cLast != null)
            {
                print 
    '</ul>' PHP_EOL;
            }
            print 
    '<ul>' PHP_EOL;
            
    $cLast $narray[$i][0];
        }
        
    printf("\t<li><a href=\"%s%s\">%s</a></li>" PHP_EOL$path$narray[$i], $name['filename']);

    Try that, works okay in my head.

    Alternatives include creating an iterator out of the array, and writing a FilterIterator to match a specific letter. A loop of A-Z would create a list matching each letter A-Z.

  • Users who have thanked Fou-Lu for this post:

    xhris (01-18-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for taking the time to reply. I greatly appreciate that. I tried the code and it printed the bullets out but nothing beside them. I know it is probably me. I am not the best at php and I am still learning. So it is probably something I did with your above code. That and it is first thing in the morning.
    Last edited by xhris; 01-18-2012 at 02:11 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Replace the $name['filename'] in the printf with just $narray[$i]. I didn't see a need to use pathinfo on this.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    OK did that and now have the file links showing. Now do I need to look up and research iterator function to get them grouped by letters?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by xhris View Post
    OK did that and now have the file links showing. Now do I need to look up and research iterator function to get them grouped by letters?
    No, you shouldn't need to use an Iterator on it.
    What it the current HTML source results?

  • #7
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Here is a SS of the output.

    If you look at the very end. You will see the 4 file names that are in that dir right now.
    Attached Thumbnails Attached Thumbnails Group files by first letter-testss.png  

  • #8
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry here is the HTML source for that code.

    Code:
    
    <ul>
    	<li><a href="./Employee Phone List 2011.pdf">Employee Phone List 2011.pdf</a></li>
    </ul>
    <ul>
    	<li><a href="./Osha.pdf">Osha.pdf</a></li>
    </ul>
    <ul>
    	<li><a href="./phone Menu.pdf">phone Menu.pdf</a></li>
    </ul>
    <ul>
    	<li><a href="./Speed Dial's with vendor Revised.pdf">Speed Dial's with vendor Revised.pdf</a></li>
    </ul>
    <ul>
    	<li><a href="./Vendor Speed Dial 2011.pdf">Vendor Speed Dial 2011.pdf</a></li>

  • #9
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Fou-Lu, I am sorry but I might not have explained what I was trying to do that great.

    I was trying to have it so that the code generates the letter A and then puts all the files that start with A under it and so on for each letter of the alphabet.

    I apologize if I wasted your time.

    Thanks for your help so far. Really appreciate this.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Are you wanting all the letters to show even if there is no entry below it?

    Edit:
    BTW, minus the missing </ul> that I overlooked, this is functioning exactly as you want it to.

  • #11
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    No if I can only show a letter when it has a file below it would be great. Is that an easy task. I do appreciate the help. Sorry for being so confusing.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by xhris View Post
    No if I can only show a letter when it has a file below it would be great. Is that an easy task. I do appreciate the help. Sorry for being so confusing.
    That would be an easier task yes; its slightly trickier to limit the looping compared to when needing all letters.
    PHP Code:
    $cLast null;
    $iTotal count($narray);

    if (
    $iTotal 0)
    {
        for (
    $i 0$i $iTotal; ++$i)
        {
            if (
    $narray[$i][0] != $cLast)
            {
                if (
    $cLast != null)
                {
                    print 
    '</ul>' PHP_EOL;
                }
                
    $cLast $narray[$i][0];
                
    printf('<h3>%s</h3>' PHP_EOLstrtoupper($cLast));
                print 
    '<ul>' PHP_EOL;
            }
            
    printf("\t<li><a href=\"%s%s\">%s</a></li>" PHP_EOL$pathurlencode($narray[$i]), $narray[$i]);
        } 

        print 
    '</ul>';


  • Users who have thanked Fou-Lu for this post:

    xhris (01-18-2012)

  • #13
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Awesome thanks for that. I am still learning and will study that code and learn it.

    Really appreciate the help!

  • #14
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sorry but I do have one question. When I add more files lets say

    a.txt and Afile.txt

    It creates a this:

    A

    Afile.txt

    A

    a.txt

    It is creating two groups depending on the letter case.


  •  

    Posting Permissions

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