PDA

Click to See Complete Forum and Search --> : Sort filenames alphabetically


usamaalam
Feb 26th, 2006, 01:53 PM
Hello everybody,

Here's my code to scan a particular directory and show all files with a checkbox next to each filename. My client just want to show these filenames sorted alphabetically.


$dir = "../download/";
$i=0;
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
print "<tr><td height='25'></td></tr>";
while (($file = readdir($dh)) != false) {
// print "filename: $file : filetype: " . filetype($dir . $file) . "\n";
if(filetype($dir . $file)!='dir' and !in_array($file,$att))
{
$i++;
//echo "";
print "<tr><td class='caption'>" . "<a href='$dir$file'>" . $file . "</td>";
if($_GET[id]=="admin")
print"<td class='caption'><input type='checkbox' name='chk$i' value='$file'></td>";
else
print"<td class='caption'></td>";
};
}
closedir($dh);
}
}



Thanks.

NoteMe
Feb 26th, 2006, 02:01 PM
When you have all the file names in an array, then use the sort function:

http://ch2.php.net/sort


- ии -

usamaalam
Feb 26th, 2006, 02:30 PM
Can you tell me how to create a dynamic array in PHP as I don't know how many files will be present in folder.

Thanks.

visualAd
Feb 26th, 2006, 02:59 PM
All arrays in PHP are dynamic, just addand remove as and when you require.

deranged
Feb 26th, 2006, 08:19 PM
lets work with this:


if(filetype($dir . $file)!='dir' and !in_array($file,$att))
{
$i++;
//echo "";
print "<tr><td class='caption'>" . "<a href='$dir$file'>" . $file . "</td>";
if($_GET[id]=="admin")
print"<td class='caption'><input type='checkbox' name='chk$i' value='$file'></td>";
else
print"<td class='caption'></td>";
};


First of all, I am not sure exactly why the last semicolon is there. so I'm going to take it out of mine.

Here's an idea of what to do:


if(!isdir($file) && !in_array($file,$att))
{
$filearray[] = $file;
}


After that you can display them.

usamaalam
Feb 27th, 2006, 03:11 AM
I populated a two dimensional array with filenames and directory names. Its working fine, I checked by printing them on page. But the sort function is not working well. How can I sort a two dimensional string array with respect to first dimension?

Thanks.

AWC_Joe
Feb 27th, 2006, 03:53 AM
Maybe: http://us3.php.net/manual/en/function.array-multisort.php

Which could have been found in NoteMe's link also.


_