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 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2003
    Location
    London
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sort files by name

    So far I have:

    PHP Code:
    <?php 


    $lstFileNames
    =array();
    $lstFileTitles=array();
    $file_names = array();
    $lines file ('/path/to/files/true_names.txt');
    foreach (
    $lines as $line_num => $line)
    {
    $parts explode('|',$line);
    $file_names[$parts[0]] = $parts[1];
    }



    if (
    $handle opendir('/path/to/files/midis/')) {
        while (
    false !== ($file readdir($handle))) { 
            if (
    $file != "." && $file != "..") { 


    echo 
    "    <tr>";
    echo 
    "        <td valign=\"top\" colspan=\"3\" width=\"95%\"><b><font color='#6B7B90'>" $file_names[$file] . "</font></b></td>";
    echo 
    "    </tr>";




    if (
    $handle2 opendir('/path/to/files/midis/' $file)) {
        while (
    false !== ($file2 readdir($handle2))) { 
            if (
    $file2 != "." && $file2 != ".." && $file2 != "index.html") { 




    $filename '/path/to/files/midis/' $file '/' $file2;




    echo 
    "    <tr>";
    echo 
    "        <td valign=\"top\" width=\"65%\"><a href=\"http://www.thedevilspride.com/files/midis/$file/$file2\">";
    echo 
    "        " $file_names[$file2] . "</a></td>";
    echo 
    "        <td width=\"5%\">&nbsp;</td>";
    echo 
    "        <td align=\"right\" valign=\"top\" width=\"35%\">" number_format(filesize($filename)/1024)." KB" "</td>";
    echo 
    "    </tr>";

            } 
        }
        
    closedir($handle2); 
    }
    echo 
    "<tr>";
    echo 
    "        <td valign=\"top\" width=\"65%\">&nbsp;</td>";
    echo 
    "        <td width=\"5%\">&nbsp;</td>";
    echo 
    "        <td align=\"right\" valign=\"top\" width=\"35%\">&nbsp;</td>";
    echo 
    "    </tr>";

            } 
        }
        
    closedir($handle); 
    }


    ?>
    At the moment they are all sorted by date.
    Does anyone know how I could sort the files by their filesnames?
    I've tried sort() but not too sure how to get it to work.

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try using ksort() rather than sort.

  • #3
    New Coder
    Join Date
    Jul 2003
    Location
    London
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've got no idea to add that to my existing code

    I've tried:

    PHP Code:
    if ($handle2 opendir('/path/to/files/midis/' $file)) {
        while (
    false !== [b](ksort($file2 readdir($handle2))))[/b] { 
            if (
    $file2 != "." && $file2 != ".." && $file2 != "index.html") { 
    also tried adding
    PHP Code:
    ksort($file2); 
    under the above code

    Arrays (and sorting them) aren't my speciality

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $lstFileNames=array();
    $lstFileTitles=array();
    $file_names = array();
    $lines file ('/path/to/files/true_names.txt');
    foreach (
    $lines as $line_num => $line)
    {
    $parts explode('|',$line);
    $file_names[$parts[0]] = $parts[1];
    }

    ksort($file_names); 
    should put them in alphanumeric, then you can print the filenames from that array.


  •  

    Posting Permissions

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