doubledee Posted March 31, 2012 Share Posted March 31, 2012 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"?? Thanks, Debbie Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted March 31, 2012 Share Posted March 31, 2012 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> Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 1, 2012 Share Posted April 1, 2012 You would replace 1980 with your set variable. Something like $dob, etc. so this isn't an option then? Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2012 Share Posted April 1, 2012 You would replace 1980 with your set variable. Something like $dob, etc. Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 You would replace 1980 with your set variable. Something like $dob, etc. That code won't work properly. Debbie Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 1, 2012 Share Posted April 1, 2012 ok, so what condition are you using to atempt to define the condition of which year will be "sticky" then? Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 1, 2012 Share Posted April 1, 2012 which minus the isset part (which is a check you didn't mention anything about in the OP) is so very close to the code provided by spfoonnewb... Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2012 Share Posted April 1, 2012 for($year = $newestYear; $year >= $oldestYear; $year--) { $selected = (isset($birthYear) && $birthYear == $year) ? ' selected="selected"' : ''; echo "<option value='{$year}'{$selected}>{$year}</option>\n"; } Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2012 Share Posted April 1, 2012 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. Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2012 Share Posted April 1, 2012 for($year = $newestYear; $year >= $oldestYear; $year--) { $selected = (isset($birthYear) && $birthYear == $year) ? ' selected="selected"' : ''; echo "<option value='{$year}'{$selected}>{$year}</option>\n"; } Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 1, 2012 Share Posted April 1, 2012 BTW, what do your curly braces mean?? What are they doing?? http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing Quote Link to comment Share on other sites More sharing options...
doubledee Posted April 1, 2012 Author Share Posted April 1, 2012 BTW, what do your curly braces mean?? What are they doing?? http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing Okay, thanks again! Debbie Quote Link to comment 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.