...

View Full Version : Group files by first letter



xhris
01-17-2012, 10:48 PM
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
$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.

Fou-Lu
01-17-2012, 11:41 PM
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.


$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.

xhris
01-18-2012, 03:02 PM
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.

Fou-Lu
01-18-2012, 03:40 PM
Replace the $name['filename'] in the printf with just $narray[$i]. I didn't see a need to use pathinfo on this.

xhris
01-18-2012, 03:44 PM
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?

Fou-Lu
01-18-2012, 03:47 PM
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?

xhris
01-18-2012, 03:51 PM
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.

xhris
01-18-2012, 04:48 PM
Sorry here is the HTML source for that 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>

xhris
01-18-2012, 05:15 PM
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.

Fou-Lu
01-18-2012, 07:05 PM
Are you wanting all the letters to show even if there is no entry below it?


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

xhris
01-18-2012, 07:13 PM
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.

Fou-Lu
01-18-2012, 08:10 PM
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.


$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_EOL, strtoupper($cLast));
print '<ul>' . PHP_EOL;
}
printf("\t<li><a href=\"%s%s\">%s</a></li>" . PHP_EOL, $path, urlencode($narray[$i]), $narray[$i]);
}

print '</ul>';
}

xhris
01-18-2012, 08:20 PM
Awesome thanks for that. I am still learning and will study that code and learn it.

Really appreciate the help!

xhris
01-18-2012, 08:24 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum