I am trying to order by date DESC some events read from a CSV and print out them.

The CSV file has to be read until it reaches the end and then order by date DESC only the entries in the following categories: champions and euro.

This is the code I wrote but I cannot order the results. Any help would be very appreciated, because I am really stucked on this.

$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++;
        }
    }
}

Recommended Answers

All 10 Replies

there is another PHP function for sorting called uksort.

function syntax is pretty self explanatory.

bool uksort ( array &$array , callable $key_compare_func )

Thank you for the hint, but unfortunately I really can't figure out how to apply to my code!

Member Avatar for diafol

Perhaps if you gave us a sample of the csv file, we could suggest a method?

Here it is

aupt;Year;Kind;When;Time;Match
en;2013;premier;18.01.2013;18.00;chelsea-m.utd
int;2014;champions;12.05.2014;;real-barca
int;2014;champions;22.08.2014;20.45;atletico-milan
de;2014;bundesliga;13.05.2014;15.00;borussia-bayern
en;2014;premier;28.03.2014;18.00;chelsea-everton
eu;2014;euro;13.03.2014;20.45;juventus-trabzonspor
de;2014;bundesliga;28.06.2014;16.00;bayern-borussia
eu;2014;euro;11.02.2014;20.45;juventus-trabzonspor
int;2014;champions;22.08.2014;20.45;atletico-milan
eu;2014;euro;13.03.2014;20.45;juventus-trabzonspor

and using this code, the order with sort() works, but ....

while ( ( $row = fgetcsv($handle, $length, $delimiter) ) != FALSE) {
    if( ($row[2] == "champions" || $row[2] == "euro") ) {
        $date = date_create($row[3]);   
        $data[] = date_timestamp_get($date);
        $rows[] = $row;
    }
}

asort($data);
fclose($handle);

print_r($data);

but if I want to print all fields ordered with asort() with a foreach() it does not work!

I mean something like:

foreach(...... as $entry) {
    echo date('M. ' . 'Y', strtotime($entry[3])) . "<br/>";
    echo date('d', strtotime($entry[3])) . "<br/>";
    echo $entry[4] . "<br/>";
    echo "<br/>";
    echo $entry[5];
}

Thank you in advance!

Member Avatar for diafol

You can try this ...

<?php
$stored_array = array();
$row = 1;
if (($handle = fopen("sample.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
       if($data[2] == 'champions' || $data[2] == 'euro'){
           if(isset($data[5]))
           {
                $time = $data[4];
                $match = $data[5];
           }else{
                $time = '';
                $match = $data[4];
           }
           $stored_array[] = array('aupt'=>$data[0],'year'=>$data[1],'kind'=>$data[2],'date'=>date('Y-m-d',strtotime($data[3])),'time'=>$time,'match'=>$match);  
        }
    }
    fclose($handle);
}

foreach ($stored_array as $key => $row) {
    $date[$key]  = $row['date'];
}

array_multisort($date, SORT_DESC, $stored_array); //or set to SORT_ASC

echo '<pre>';
print_r($stored_array);
echo '</pre>';
?>

Awesome.....just last question! I start to feel a little dump, but if I want to print each entry in a different div, like:

echo date('M. ' . 'Y', strtotime($entry[3])) . "<br/>";
echo date('d', strtotime($entry[3])) . "<br/>";
echo $entry[4] . "<br/>";
echo "<br/>";
echo $entry[5];

So sorry, but I am really out of mind!

Member Avatar for diafol

What do you mean each entry? As a caledar?

I mean print as a list, like:

The match will be played the: .......
At: ..........
The match is between:.........

this obviously printed for each match.

Member Avatar for diafol

OK, still a little unsure. You have the data in the array and you have some text.

$output = '';
foreach($stored_array as $match)
{
    $output .= "<div><p>The match will be played on: {$match['date']}</p>
                    <p>At: {$match['time']}</p>
                    <p>The match is between: {$match['match']}</p></div>";
}

echo $output;
commented: Awesome and PATIENT guy. Thumbs up +0

Really cannot tell you how much I am thankful to you!

Thank you very much for the patience. Really appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.