Page 1 of 1
Auto selecting drop down menu
Posted: Sun May 31, 2009 6:46 pm
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>
Re: Auto selecting drop down menu
Posted: Sun May 31, 2009 7:11 pm
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
Re: Auto selecting drop down menu
Posted: Mon Jun 01, 2009 12:05 pm
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?
Re: Auto selecting drop down menu
Posted: Mon Jun 01, 2009 12:12 pm
by iFlex
Why dont you try coding all the months and what it selects to as variables?
Re: Auto selecting drop down menu
Posted: Mon Jun 01, 2009 2:25 pm
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]...
Re: Auto selecting drop down menu
Posted: Mon Jun 01, 2009 3:09 pm
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?
Re: Auto selecting drop down menu
Posted: Tue Jun 02, 2009 1:36 am
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!
Re: Auto selecting drop down menu
Posted: Thu Jun 04, 2009 2:53 pm
by tomsace
Any ideas anyone? I still haven't figured this one out!
Re: Auto selecting drop down menu
Posted: Thu Jun 04, 2009 3:30 pm
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.