Auto selecting drop down menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Auto selecting drop down menu

Post by tomsace »

Hey,
I have been using the following code to auto select the option stored in a mysql result:

Code: Select all

<select name="month" class="input" style="width: 76px;">
<option value="January" <?php if($line['month'] == "January") print "selected"; ?>>January</option>
</select>
How would I use this method or something similar on a drop down menu for a year selection? Heres my code:

Code: Select all

<select name="year" class="input" style="width: 50px;">
<?php $year = date("Y") + 1; for ($i = 0; $i <= 100; $i++) {$year--; echo "<option>$year</option>";} ?>
</select>
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Auto selecting drop down menu

Post by mikemike »

Code: Select all

<select name="year" class="input" style="width: 50px;">
   <?php $year = date("Y") + 1; for ($i = 0; $i <= 100; $i++) {$year--; echo "<option".($year == $the_year_you_have_stored_goes_here ? ' selected="selected"' : '')>$year</option>";} ?>
</select>
Notice the variable to change
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Auto selecting drop down menu

Post by tomsace »

Hey,
Can't seem to get this code to work, been messing around with it for a good hour now but no luck :\ I can't find the error?
User avatar
iFlex
Forum Commoner
Posts: 41
Joined: Sat May 30, 2009 3:44 am

Re: Auto selecting drop down menu

Post by iFlex »

Why dont you try coding all the months and what it selects to as variables?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Auto selecting drop down menu

Post by tomsace »

I have done, I have only posted the one month as an example.

I just need help auto selecting the year. By the way I am getting the year from the variable $line[year]...
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Auto selecting drop down menu

Post by mikemike »

I'm assuming you've read my code and changed the obvious named variable "$the_year_you_have_stored_goes_here" to $line[year] with the appropriate escaping?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Auto selecting drop down menu

Post by tomsace »

Yeah! But it just errors, the page stops loading when it gets to the script, I have tried changing a few things around but no luck. Is there any errors in the script? I can't find them!
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: Auto selecting drop down menu

Post by tomsace »

Any ideas anyone? I still haven't figured this one out!
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Auto selecting drop down menu

Post by mikemike »

When you say it errors, what does it say?

If you get a blank page make sure display_errors is on and error_reporting is set to something sensible, then give us the errors. Should be pretty easy to track down.
Post Reply