Jump to content

Need help with conditional to determine if month listed is current month....


tracedef

Recommended Posts

We have two select boxes in a form, one lists months and one lists years. We want use a conditional statement to test to see if each month / year option in each select box is the current month or year and then apply a selected="selected" attribute if it is. This will enable the select boxes to always default to current month / year.

 

I'm not quite sure where to even start in terms of php. We're looking to test if option value is equal to current month, if so, apply selected="selected", otherwise do nothing....

 

 

One section of select box:

 

<option label="Jan" value="1" selected="selected">Jan</option>

 

Here is a yearly example:

 

<option label="2010" value="2010" selected="selected">2010</option>

 

 

One select box currently looks like this:

 

<select name="sm"> <option label="Jan" value="1">Jan</option> <option label="Feb" value="2" [color=red]selected="selected"[/color]>Feb</option> <option label="Mar" value="3">Mar</option> <option label="Apr" value="4">Apr</option> <option label="May" value="5">May</option> <option label="Jun" value="6">Jun</option> <option label="Jul" value="7">Jul</option> <option label="Aug" value="8">Aug</option> <option label="Sep" value="9">Sep</option> <option label="Oct" value="10">Oct</option> <option label="Nov" value="11">Nov</option> <option label="Dec" value="12">Dec</option> </select>

 

Any feedback greatly appreciated!

 

Link to comment
Share on other sites

well first off get the current month and year.

$month = date('n');
$year = date('Y');

 

Then add this to your month select option:

<option label="Jan" value="1" <?php echo ($month==1) ? "selected='selected'" : ""; ?>>Jan</option>

 

1 will change to 2 and etc etc as you go thru the months.

 

year is the exact same as thing.

 

let me know if you have any questions.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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