Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

using ORDER BY to get months in order

Featured Replies

I am trying to print the months out in month order rather than alphabetically.

 

They are in the database as words, not numbers.

 

Can I use the ORDER BY clause to force the order to be Jan Feb etc?

 

This is what I have so far but this is alphabetical:

 

$q = "SELECT month, day, band_name, venue, price FROM gigs ORDER BY month, day";

 

Any help would be great ...

I am trying to print the months out in month order rather than alphabetically.

 

They are in the database as words, not numbers.

 

Can I use the ORDER BY clause to force the order to be Jan Feb etc?

 

This is what I have so far but this is alphabetical:

 

$q = "SELECT month, day, band_name, venue, price FROM gigs ORDER BY month, day";

 

Any help would be great ...

 

MYSQL orders alphabetically in this case, so August would come first.

 

You have 2 options.

 

1) Change your script so the month is saved to the database as the number.

 

2) Create an array associating the number of the month with the name and dump all the mysql results into an array and order it that way.

 

Number 1 is a lot easier.

  • Author

bummer

 

I thought that might be the best way ... will get onto it ... sometime ...

 

thanks very much

  • Author

Ok, I have changed the input to the database to numbers 1-12 for each month. Now when I display them I want to show January not 1, what is the simplest code for this?

 

This is what I have so far:

 

$q = "SELECT month, day, band_name, venue FROM gigs ORDER BY month, day";

$r = mysqli_query($dbc, $q);

 

$currentGenre = '';

 

while ($messages = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

 

if ($currentMonth != $messages['month']) {

 

echo "<h2>{$messages['month']}</h2>\n";

$currentMonth = $messages['month'];

}

 

But it is printing 1 not January etc ...

 

Thanks for any help ...

You could try having another column in your table that stores the month numbers. So you have one for the month text and one for the month number. Then you can order by the month number and echo the month text. I hope this makes sense.

What Mark said or.

 

<?php
$months = array('1' => 'January', '2' => 'February, ...etc );
?>

 

Then for your echo/print have:

 

$thismonth = $months[$month_number];

  • Author
Then for your echo/print have:

 

$thismonth = $months[$month_number];

 

Don't I have to define what the $month_number represents somewhere? (if so how?)

Don't I have to define what the $month_number represents somewhere? (if so how?)

$month_number would be the numerical output from the database for the month.

 

$thismonth = $months[$month_number];

 

This would then take the $month_number from the database, then display the text in the array relating to the number $month_number.

  • Author

Sorry for being so dim on this, but I still can't get it working. Here is what I have so far but it is erroring (does not like '$month'). The table is called 'gigs' and the month is stored in the 'month' column as a number:

require_once (MYSQL); // Connect to the db.

$q = "SELECT month, day, band_name, venue, price, tickets_from, on_the_door FROM gigs ORDER BY month, day";
$r = mysqli_query($dbc, $q);

$months = array('1' => 'January', '2' => 'February', '3' => 'March', '4' => 'April', '5' => 'May', '6' => 'June', '7' => 'July', '8' => 'August', '9' => 'September', '10' => 'October', '11' => 'November', '12' => 'December');



$currentMonth = '';

while ($messages = mysqli_fetch_array($r, MYSQLI_ASSOC)) {


   if ($currentMonth != $messages[$month]) {

       echo "<h2>{$messages[$month]}</h2>\n";
       $currentMonth = $messages[$month];
   }

   echo "    
{$messages['day']}   
".stripslashes($messages['band_name'])."
, ".stripslashes($messages['venue'])." ";



if($messages['price']=='0'){ 
echo ", FREE";
}

 

Again, thanks for everything

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.