0

I am trying to order by date DESC the events read from a CSV and print them in a DIV. The file has to be read till its end and then order by date DESC only the entries in the following categories: champions and euro.

This is the code I wrote, checking also a thread here on StackOverflow, but I cannot order the results. Any help would be very appreciated!

$today = date('Y-m-d', strtotime("now"));
$header = fgetcsv($handle);
$length = 1000;
$delimiter = ";";

$date = array();
$rows = array();

while ( ( $row = fgetcsv($handle, $length, $delimiter) ) != FALSE) {        
    if( ($row[2] == "champions" || $row[2] == "euro") ) {
        // if there is a , in the date take the first part of it
        preg_match('~([^\s]+)(?:,.*)?$~', $row[3], $m); // This is probably wrong (preg_match drives me crazy)
        $date[] = $m[1];
        $rows[] = $row;
    }
}

fclose($handle);

array_multisort($date, $rows);

foreach ($rows as $entry) {
    $csv_date = date('Y-m-d', strtotime($entry[3]));

    if($csv_date >= $today) {
        if( ($i == 0 || $i <= 2) ) {
            echo date('M. ' . 'Y', strtotime($entry[3])) . "<br/>";
            echo date('d', strtotime($entry[3])) . "<br/>";
            echo "<br/>";
            // Print only 3 matches
            echo $entry[8];

            $i++;
        }
    }
}
Mark
  • 1,069
  • 2
  • 21
  • 44

0 Answers0