Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/205603-help-with-dynamic-dropdown-and-database/
Share on other sites

<?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>";
}

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.

Here's the final code in case anyone is interested.  8)

 

<?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>";
}

?>

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.