Jump to content

results are sorted by directory name and not date


Tim32
Go to solution Solved by kicken,

Recommended Posts

I am trying create a webpage that will automatically display new agendas as I add them to the server.  I do this right now but I have to put the agendas in the agendas and the board folders.  I want to simplify this by only putting the agendas in the board folder.  My code kinda works but the results aren't sorted correctly.  My code scans the current directory for the list of current boards.  The arrary_diff removes the boards that are no longer active.  It then appends the current year to the folder information.  Next I glob all of the Agendas in the various directories and then I sort the resulting glob.  Finally I print the results which are out of date order.  I tried sort(basename($agendas)) but that is not valid syntax for sort.  Then I was thinking of a 2 dimension array, the first element is the filename, the second element is the fullpath.  Then I sort on the first element but I haven't figured out how to get that to work.  So I went back to my first code and I am asking what did I do wrong?  Am I on the right track to do what I want?

The directory structure is like this:

Array (
    [0] => ./meeting-minutes-agendas/Board_of_Zoning_Appeals/2021/01-17-2021_Board_of_Zoning_Appeals_Agenda.pdf
    [1] => ./meeting-minutes-agendas/Board_of_Zoning_Appeals/2021/02-24-2021_Board_of_Zoning_Appeals_Agenda.pdf
    [2] => ./meeting-minutes-agendas/Board_of_Zoning_Appeals/2021/03-17-2021_Board_of_Zoning_Appeals_Agenda.pdf
    [3] => ./meeting-minutes-agendas/Board_of_Zoning_Appeals/2021/04-21-2021_Board_of_Zoning_Appeals_Agenda.pdf
    [4] => ./meeting-minutes-agendas/Economic_Development_Committee/2021/01-24-2021_EDC_Agenda.pdf
    [5] => ./meeting-minutes-agendas/Economic_Development_Committee/2021/02-21-2021_EDC_Agenda.pdf
    [6] => ./meeting-minutes-agendas/Economic_Development_Committee/2021/03-21-2021_EDC_Agenda.pdf
    [7] => ./meeting-minutes-agendas/Park_Board/2021/01-21-2021_Park_Board_Agenda.pdf
    [8] => ./meeting-minutes-agendas/Park_Board/2021/02-18-2021_Park_Board_Agenda.pdf
    [9] => ./meeting-minutes-agendas/Park_Board/2021/03-18-2021_Park_Board_Agenda.pdf
    [10] => ./meeting-minutes-agendas/Park_Board/2021/03-27-2021_Park_Board_Agenda.pdf
    [11] => ./meeting-minutes-agendas/Park_Board/2021/04-22-2021_Park_Board_Agenda.pdf
    [12] => ./meeting-minutes-agendas/Plan_Commission/2021/01-07-2021_Plan_Commission_Agenda.pdf
    [13] => ./meeting-minutes-agendas/Plan_Commission/2021/01-21-2021_Plan_Commission_Agenda.pdf
    [14] => ./meeting-minutes-agendas/Plan_Commission/2021/02-04-2021_Plan_Commission_Agenda.pdf
    [15] => ./meeting-minutes-agendas/Plan_Commission/2021/02-18-2021_Plan_Commission_Agenda.pdf
    [16] => ./meeting-minutes-agendas/Plan_Commission/2021/03-04-2021_Plan_Commission_Agenda.pdf
    [17] => ./meeting-minutes-agendas/Plan_Commission/2021/03-18-2021_Plan_Commission_Agenda.pdf
    [18] => ./meeting-minutes-agendas/Plan_Commission/2021/04-08-2021_Plan_Commission_Agenda.pdf
    [19] => ./meeting-minutes-agendas/Plan_Commission/2021/04-22-2021_Plan_Commission_Agenda.pdf
    [20] => ./meeting-minutes-agendas/Redevelopment_Commission/2021/01-17-2021_RDC_Agenda.pdf
    [21] => ./meeting-minutes-agendas/Redevelopment_Commission/2021/02-14-2021_RDC_Agenda.pdf
    [22] => ./meeting-minutes-agendas/Redevelopment_Commission/2021/03-14-2021_RDC_Agenda.pdf
    [23] => ./meeting-minutes-agendas/Safety_Board/2021/01-08-2021_Safety_Board_Agenda.pdf
    [24] => ./meeting-minutes-agendas/Safety_Board/2021/02-16-2021_Safety_Board_Agenda.pdf
    [25] => ./meeting-minutes-agendas/Safety_Board/2021/03-16-2021_Safety_Board_Agenda.pdf
    [26] => ./meeting-minutes-agendas/Safety_Board/2021/04-22-2021_Safety_Board_Agenda.pdf
    [27] => ./meeting-minutes-agendas/Sanitary_District/2021/01-19-2021_Sanitary_District_Agenda.pdf
    [28] => ./meeting-minutes-agendas/Sanitary_District/2021/02-16-2021_Sanitary_District_Agenda.pdf
    [29] => ./meeting-minutes-agendas/Sanitary_District/2021/03-16-2021_Sanitary_District_Agenda.pdf
    [30] => ./meeting-minutes-agendas/Sanitary_District/2021/03-23-2021_Sanitary_District_Agenda.pdf
    [31] => ./meeting-minutes-agendas/Sanitary_District/2021/03-31-2021_Sanitary_District_Agenda.pdf
    [32] => ./meeting-minutes-agendas/Sanitary_District/2021/04-20-2021_Sanitary_District_Agenda.pdf
    [33] => ./meeting-minutes-agendas/Town_Council/2021/01-13-2021_Town_Council_Agenda.pdf
    [34] => ./meeting-minutes-agendas/Town_Council/2021/01-27-2021_Town_Council_Agenda.pdf
    [35] => ./meeting-minutes-agendas/Town_Council/2021/02-02-2021_Town_Council_Agenda.pdf
    [36] => ./meeting-minutes-agendas/Town_Council/2021/02-24-2021_Town_Council_Agenda.pdf
    [37] => ./meeting-minutes-agendas/Town_Council/2021/03-03-2021_Town_Council_Agenda.pdf
    [38] => ./meeting-minutes-agendas/Town_Council/2021/03-24-2021_Town_Council_Agenda.pdf
    [39] => ./meeting-minutes-agendas/Town_Council/2021/04-14-2021_Town_Council_Special_Meeting_Agenda.pdf
    [40] => ./meeting-minutes-agendas/Town_Council/2021/04-14-2021_Town_Council_Study_Session_Agenda.pdf
    [41] => ./meeting-minutes-agendas/Waterworks_Board/2021/01-07-2021_Waterwork_Board_Agenda.pdf
    [42] => ./meeting-minutes-agendas/Waterworks_Board/2021/01-19-2021_Waterworks_Board_Agenda.pdf
    [43] => ./meeting-minutes-agendas/Waterworks_Board/2021/01-28-2021_Waterworks_Board_Agenda.pdf
    [44] => ./meeting-minutes-agendas/Waterworks_Board/2021/02-16-2021_Waterworks_Board_Agenda.pdf
    [45] => ./meeting-minutes-agendas/Waterworks_Board/2021/03-16-2021_Waterworks_Board_Agenda.pdf
    [46] => ./meeting-minutes-agendas/Waterworks_Board/2021/04-20-2021_Waterworks_Board_Agenda.pdf
)

What I get is

04-21-2021_Board_of_Zoning_Appeals_Agenda.pdf
04-22-2021_Park_Board_Agenda.pdf
04-22-2021_Plan_Commission_Agenda.pdf
04-22-2021_Safety_Board_Agenda.pdf
04-20-2021_Sanitary_District_Agenda.pdf
04-20-2021_Waterworks_Board_Agenda.pdf

when it should be:

04-20-2021_Sanitary_District_Agenda.pdf
04-20-2021_Waterworks_Board_Agenda.pdf
04-21-2021_Board_of_Zoning_Appeals_Agenda.pdf
04-22-2021_Park_Board_Agenda.pdf
04-22-2021_Plan_Commission_Agenda.pdf
04-22-2021_Safety_Board_Agenda.pdf

<?php
						$files = array_diff(scandir('./meeting-minutes-agendas'), array('..', '.', 'Default.php', 'Agendas', 'Police_Commission', 'Utility_Board'));
						sort($files);
						foreach($files as &$file) {
							$file = $file . "/" . date("Y");
						}
						unset($file);
						$agendas = array();
						foreach($files as $file) {
							$temp = glob('./meeting-minutes-agendas/'. $file . "/*Agenda.pdf");
							$agendas = array_merge($agendas, $temp);
						}
						unset($files);
						unset($temp);
						sort($file);
						foreach($agendas as $file) {
							$filedate = str_replace('-', '/',substr(basename($file),0,10));
							if (strtotime($filedate) >= strtotime(date('m/d/Y'))) {
								echo "<a href='";
								echo $file;
								echo "'>";
								$file = str_replace('.php', '', $file);
								echo basename($file);
								echo "</a><br>\n";
							}
						}
						unset($file);
				  ?>

 

Link to comment
Share on other sites

  • Solution

Use usort rather than sort, then you can provide a comparison function and implement whatever logic you need to get the correct sorting.  The comparison function will be given two items from the array ($a and $b) and must return a number that indicates if $a is less than $b (-1), they are equal (0), or $a is greater than $b (1).

If your files all began with the date in Y-m-d format you'd be able to simply compare them.  In m-d-Y format though you need to parse the date out of the name and then compare the date.

usort($files, function($a, $b){
    $a = basename($a);
    $b = basename($b);
    $aDate = DateTime::createFromFormat('!m-d-Y+', $a);
    $bDate = DateTime::createFromFormat('!m-d-Y+', $b);

    if ($aDate && $bDate){
        $comparison = $aDate <=> $bDate;
        if ($comparison === 0){
            return $a <=> $b;
        } else {
            return $comparison;
        }
    } else {
        return $a <=> $b;
    }
});

Here, the function reduces each path given to it's basename, then attempts to parse a date in m-d-Y format from the beginning of the filename.  If it's able to successfully parse a date from both filenames then it compares the dates to get that -1, 0, or 1 value indicating their order.  If the result is 0, meaning the dates are equal, it compares the entire string and returns that order.  If it's unable to parse a date from either of the filenames then it just returns an order based on a string comparison.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.