2

I am trying to sort a list of directories according to the contents of a text file within each directory.

So far, I am displaying the list of directories:

$user = $_GET['user'];
$task_list = $_GET['list'];

if ($handle = opendir("../users/$user/tasks/$task_list/"))
{
    $files = array();
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != ".." && $file != ".htaccess")
        {
            array_push($files, $file);
        }
    }
    closedir($handle);
}

//Display tasks
sort($files);
foreach ($files as $file)
{
    echo "$file"; 
}

Each directory has a text file within it called due.txt, I would like to sort the list of directories according to the contents of this file due.txt.

So far, I have tried:

$user = $_GET['user'];
$task_list = $_GET['list'];

if ($handle = opendir("../users/$user/tasks/$task_list/"))
{   
    $files = array();   
    $tasksSort = array();
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != ".." && $file != ".htaccess")
        {
            $taskSort = file_get_contents("../users/$user/tasks/$task_list/$file/due.txt");
            array_push($files, $file);
            array_push($tasksSort, $taskSort);
        }
        closedir($handle);
    }

    //Sort tasks and display
    sort($tasksSort);
    foreach ($files as $file)
    {
        echo "$file"; 
    }
}

But the $tasksSort array doesn't seem to have any content to sort...?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Oscar Strangio
  • 149
  • 2
  • 2
  • 7
  • possible duplicate of [Sort and display directory list alphabetically using opendir() in php](http://stackoverflow.com/questions/884974/sort-and-display-directory-list-alphabetically-using-opendir-in-php) – John Conde Feb 03 '13 at 01:45

1 Answers1

1
sort($tasksSort);
foreach ($tasksSort as $file)
{
    echo "$file"; 
}
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
keyboardSmasher
  • 2,661
  • 18
  • 20