Jump to content

Sort by Month


oldschool

Recommended Posts

You didn't provide the context around this sorting thing you want. Is data in MySQL, in a file, or just in an array?

If it's MySQL, basically you can use the "ORDER BY" clause to sort in MySQL.

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]table_name[/color] [color=green]ORDER BY[/color] YEAR(date_column) ASC, MONTH(date_column) [color=green]ASC[/color] [!--sql2--][/div][!--sql3--]


Select syntax showing order by clause:
[a href=\"http://dev.mysql.com/doc/refman/4.1/en/select.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/select.html[/a]

Date and time functions in MySQL:
[a href=\"http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/dat...-functions.html[/a]
Link to comment
https://forums.phpfreaks.com/topic/8769-sort-by-month/#findComment-32200
Share on other sites

Thanks ^

Sorry, yes, it's MySQL

How would I combine the above with this

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]SELECT DISTINCT MONTHNAME( gig_date ) AS MONTH FROM gigs WHERE (gig_date >= CURDATE( )) ORDER BY MONTH DESC[/quote]

which will return the months listed in the database.

So for example, the above currently lists April & May because the records contain dates in these months
How can I attach each record(s) to each month?
Link to comment
https://forums.phpfreaks.com/topic/8769-sort-by-month/#findComment-32204
Share on other sites

It looks like the table holds events and when they occur. So, you probably don't want to use distinct. Example:

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] *, MONTHNAME(`gig_date`) [color=green]AS[/color] gig_month_name, MONTH(`gig_date`) [color=green]AS[/color] gig_month_nbr, YEAR(`gig_date`) [color=green]as[/color] gig_year
[color=green]FROM[/color] [color=orange]`gigs`[/color] [color=green]WHERE[/color] (`gig_date` [color=orange]>[/color][color=orange]=[/color] CURDATE( ))
[color=green]ORDER BY[/color] gig_year ASC, gig_month_nbr [color=green]ASC[/color] [!--sql2--][/div][!--sql3--]

That SQL will return all rows sorted by year then month in ascending order. Then it's just a matter of only displaying the month and year heading once or when you want to. Like when it changes. Example:

[code]
// open, select DB, and query table using order by and put in $result

// Initialize to something that the data won't contain
$saved_year = 0;  
$saved_month = 0;

while ($row = mysql_fetch_assoc($result)) {

    // Determine if data has changed and display one time (heading)
    if (($row['gig_year'] != $saved_year) ||
        ($row['gig_month_nbr'] != $saved_month)) {

        echo $row['gig_month_name'], ' ', $row['gig_year'], '<br/>';

        $saved_year    = $row['gig_year'];
        $saved_month = $row['gig_month_nbr'];
    }

    // Display other column data
    echo $row['event'], '<br/>';

}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/8769-sort-by-month/#findComment-32267
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.