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
tomsace
Forum Contributor
Posts: 167 Joined: Thu Jan 01, 2009 8:07 pm
Post
by tomsace » Sun May 31, 2009 6:46 pm
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>
mikemike
Forum Contributor
Posts: 355 Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK
Post
by mikemike » Sun May 31, 2009 7:11 pm
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
Post
by tomsace » Mon Jun 01, 2009 12:05 pm
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?
iFlex
Forum Commoner
Posts: 41 Joined: Sat May 30, 2009 3:44 am
Post
by iFlex » Mon Jun 01, 2009 12:12 pm
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
Post
by tomsace » Mon Jun 01, 2009 2:25 pm
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]...
mikemike
Forum Contributor
Posts: 355 Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK
Post
by mikemike » Mon Jun 01, 2009 3:09 pm
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
Post
by tomsace » Tue Jun 02, 2009 1:36 am
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
Post
by tomsace » Thu Jun 04, 2009 2:53 pm
Any ideas anyone? I still haven't figured this one out!
mikemike
Forum Contributor
Posts: 355 Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK
Post
by mikemike » Thu Jun 04, 2009 3:30 pm
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.