Jump to content

Make Birth Year Sticky


doubledee

Recommended Posts

I have PHP code that generates a long list of Birth Years in a Drop-down list, and I want to make the control "sticky".

 

I know how to make something like this sticky - thanks to help from you guys...

<!-- Gender -->
<label for="gender">Gender:</label>
<select id="gender" name="gender">
	<option value="">--</option>
	<option value="F"<?php echo (isset($gender) && $gender == "F") ? 'selected="selected"' : ''; ?>>Female</option>
	<option value="M"<?php echo (isset($gender) && $gender == "M") ? 'selected="selected"' : ''; ?>>Male</option>
</select>

 

 

But my Birth Year code is a little more complex and I'm racking me brain how to do it...

<!-- Birth Year -->
<label for="birthYear">Year Born:</label>
<select id="birthYear" name="birthYear">
	<option value="">--</option>
	<?php
		// Display dates for Users between 18 and 100.
		for($i = $newestYear; $i >= $oldestYear; $i--){
			echo '<option value="' . $i . '">' . $i . '</option>';
		}
	?>
</select>

 

How do I make this second set of code "sticky"??  :shrug:

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

Something like this should work. You would replace 1980 with your set variable. Something like $dob, etc.

 

<!-- Birth Year -->
<label for="birthYear">Year Born:</label>
<select id="birthYear" name="birthYear">
    <option value="">--</option>
        <?php
        // Display dates for Users between 18 and 100.
        for($i = $newestYear; $i >= $oldestYear; $i--) {
            if ($i == 1980) {
                echo '<option value="' . $i . '" selected="selected">' . $i . '</option>';
            }
            else {
                echo '<option value="' . $i . '">' . $i . '</option>';
            }
        }
        ?>
</select>

Link to comment
Share on other sites

Something like this should work. You would replace 1980 with your set variable. Something like $dob, etc.

 

<!-- Birth Year -->
<label for="birthYear">Year Born:</label>
<select id="birthYear" name="birthYear">
    <option value="">--</option>
        <?php
        // Display dates for Users between 18 and 100.
        for($i = $newestYear; $i >= $oldestYear; $i--) {
            if ($i == 1980) {
                echo '<option value="' . $i . '" selected="selected">' . $i . '</option>';
            }
            else {
                echo '<option value="' . $i . '">' . $i . '</option>';
            }
        }
        ?>
</select>

 

I don't see how that code would work.

 

I need to make the Birth Year that the User selects "sticky".

 

You are arbitrarily making 1980 sticky, but it can't be static, it has to be based on what the User chose before submitting the form...

 

 

Debbie

 

Link to comment
Share on other sites

You would replace 1980 with your set variable. Something like $dob, etc.

so this isn't an option then?

 

I don't understand what he is telling me to do.

 

And I am saying that I need to maintain whatever value the User selects for his/her "Birth Year" in the drop-down.

 

I'm playing around with my code but not getting anywhere...

 

 

Debbie

 

 

Link to comment
Share on other sites

This code does seem to work...

<!-- Birth Year -->
<label for="birthYear">Year Born:</label>
<select id="birthYear" name="birthYear">
	<option value="">--</option>
	<?php
		// Display dates for Users between 18 and 100.
		for($i = $newestYear; $i >= $oldestYear; $i--){
			if (isset($birthYear) && $birthYear == $i){
				echo '<option value="' . $i . '" selected="selected">' . $i . '</option>';
			}else{
				echo '<option value="' . $i . '">' . $i . '</option>';
			}
		}
	?>
</select>

 

 

Debbie

 

Link to comment
Share on other sites

WHy doesn't my code create new line breaks when I choose "View Source"...

if (isset($birthYear) && $birthYear == $year){
	echo '<option value="' . $year . '" selected="selected">' . $year . '</option>\n';
}else{
	echo '<option value="' . $year . '">' . $year . '</option>\n';
}

 

 

Debbie

 

Link to comment
Share on other sites

WHy doesn't my code create new line breaks when I choose "View Source"...

if (isset($birthYear) && $birthYear == $year){
	echo '<option value="' . $year . '" selected="selected">' . $year . '</option>\n';
}else{
	echo '<option value="' . $year . '">' . $year . '</option>\n';
}

 

http://php.net/manual/en/language.types.string.php

    Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Link to comment
Share on other sites

http://php.net/manual/en/language.types.string.php

    Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

 

Can I just flip my single and double quotes?

 

And do I really even need to worry about newline breaks anyways?

 

 

Debbie

 

 

Link to comment
Share on other sites

for($year = $newestYear; $year >= $oldestYear; $year--)
{
    $selected = (isset($birthYear) && $birthYear == $year) ? ' selected="selected"' : '';
    echo "<option value='{$year}'{$selected}>{$year}</option>\n";
}

 

Okay, that works.

 

Thanks!!

 

BTW, what do your curly braces mean??

 

What are they doing??

 

 

Debbie

 

Link to comment
Share on other sites

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.