tqla Posted June 23, 2010 Share Posted June 23, 2010 Hello. I am put together this loop to display this year and the past 100 years in a dropdown menu: Year: <?php echo '<select name="dob-year">'; for ($i = 0; $i < 100; $i++) { $date_str = date('Y', strtotime("- $i years")); echo "<option value=\"$date_str\">$date_str</option>"; } echo "</select>"; ?> I want to add two things to it but everything I try fails. 1) I queried the DB first and have a var called $dob_year. If it is blank then I wish to have the "selected" item be "select" with no value <option value=" " selected>select</option> 2) If $dob_year has a year value (ie 1998) I want the dropdown to have that year "selected" and still show the other 99 so that another value can be selected. <option value="<?=$dob_year;?>" selected> <?=$dob_year;?></option> Can somebody help me do this? I think I know how to accomplish this using 100 dropdown options but I am trying to use the for loop function. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/205603-help-with-dynamic-dropdown-and-database/ Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 <?php echo '<select name="dob-year">'; for($i=0;$i<100;$i++){ $date_str = (date('Y')-$i); if($date_str == "2010"){ echo "<option value=\"$date_str\" selected>$date_str</option>"; }else{ echo "<option value=\"$date_str\">$date_str</option>"; } } echo "</select>"; ?> and the #1 you requested maybe this if($date_str == ""){ echo "<option value=" " selected>Select</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/205603-help-with-dynamic-dropdown-and-database/#findComment-1075895 Share on other sites More sharing options...
TapeGun007 Posted June 23, 2010 Share Posted June 23, 2010 I did this a little different than above. All you need to check is if the db Date is equal to the date being output. So... <?php echo '<select name="dob-year">'; for ($i = 0; $i < 100; $i++) { $date_str = date('Y', strtotime("- $i years")); If ($dob_year==$date_str{ $selected="selected" } Else{ $selected=""; } echo "<option value=\"$date_str\" $selected>$date_str</option>";}echo "</select>"; ?> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/205603-help-with-dynamic-dropdown-and-database/#findComment-1075898 Share on other sites More sharing options...
tqla Posted June 23, 2010 Author Share Posted June 23, 2010 Ah ha! Thanks Ruzzas and TapeGun007. They both work. It's interesting to see the differences. Quote Link to comment https://forums.phpfreaks.com/topic/205603-help-with-dynamic-dropdown-and-database/#findComment-1075900 Share on other sites More sharing options...
tqla Posted June 23, 2010 Author Share Posted June 23, 2010 Here's the final code in case anyone is interested. <?php if($dob_year != ""){ echo '<select name="dob-year">'; for($i=0;$i<110;$i++){ $date_str = (date('Y')-$i); if($date_str == $dob_year){ echo "<option value=\"$date_str\" selected>$date_str</option>"; }else{ echo "<option value=\"$date_str\">$date_str</option>"; } } echo "</select>"; }else{ echo '<select name="dob-year">'; for($i=0;$i<110;$i++){ $date_str = (date('Y')-$i); if($date_str == date('Y')){ echo "<option value=\"\" selected>select</option>"; echo "<option value=\"$date_str\">$date_str</option>"; }else{ echo "<option value=\"$date_str\">$date_str</option>"; } } echo "</select>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/205603-help-with-dynamic-dropdown-and-database/#findComment-1075958 Share on other sites More sharing options...
Ruzzas Posted June 23, 2010 Share Posted June 23, 2010 No problems, Glad you finished that off Quote Link to comment https://forums.phpfreaks.com/topic/205603-help-with-dynamic-dropdown-and-database/#findComment-1075963 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.