Originally posted by tigger
right I have the dropdown menu working now. Ok, the month is selected, but what I want is the list to actally start being displayed by the current month. How do I do that?
Tigger
First, get all your months into an array.
$months=array();
$sql='SELECT ....';
$result=mysql_query(...);
while(...)
$months[]=...;
(Okay, so I'm too lazy to fill in all the gaps.)
So now our array is looking like:
$months = array(0=>'January', 1=>'February', ... 11=>'December');
assuming English month names (and assuming that they're stored in the database in order, or assuming that you can come up with a suitable ORDER BY clause so that MySQL can tell which is the first month, etc.).
Find out the current month. Calling it $current_month seems like a good idea. This will have to come out of the database as well (so it will need to know which month is the first, etc.), because it's quite likely that date() won't know what language to use.
Now we've got it, we want to know where in the array it's currently sitting. That's easy (at least, it's easy enough I can think of two ways to do it straight off):
$current_month_idx = array_search($current_month,$months);
Let's do the loop:
for($i=0; $i<12; $i++) // Anyone use something other than 12 months?
{ $month = $months[($i+$current_month_idx)%12];
echo "<option>$month</option>";
}
If you're not certain about the 12-month thing, just replace 12 in the code above with the length of the $months array.
Oh, and to put the default selection in and speed things up a tick:
echo "<option selected>$current_month</option>";
for($i=1; $i<12; $i++)
{ $month = $months[($i+$current_month_idx)%12];
echo "<option>$month</option>";
}