June 29, 200818 yr 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 ...
June 29, 200818 yr 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.
June 29, 200818 yr Author bummer I thought that might be the best way ... will get onto it ... sometime ... thanks very much
June 29, 200818 yr 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 ...
June 29, 200818 yr 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.
June 29, 200818 yr What Mark said or. <?php $months = array('1' => 'January', '2' => 'February, ...etc ); ?> Then for your echo/print have: $thismonth = $months[$month_number];
June 29, 200818 yr 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?)
June 30, 200818 yr 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.
June 30, 200818 yr 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