ramone_johnny Posted May 17, 2013 Share Posted May 17, 2013 Hey guys, This is probably a complete no brainer, but I'm stuck with it. Here's the code I currently have. <select name="share_prefage" class="textarea" style="width:150px"> <? if (!isset($_SESSION['share_prefage'])) echo "<option value='Doesnt Matter'>Doesnt Matter</option>"; else { echo "<option selected value=" . $_SESSION['share_prefage'] . ">Around " . $_SESSION['share_prefage'] . "</option>"; } { for ($i=18; $i<=99; $i++) echo "<option value='$i'>Around $i</option>"; } ?> </select> Put simply here's what I'm trying to accomplish 1. If the session variable has not been set, set the default value to "Doesnt Matter. 2. If the session variable has been set, write it within the drop down BUT... if it equals "Doesnt Matter" then don't write Doesnt Matter twice, and dont write "Around Doesnt Matter" 3. Keep the loop at the end regardless. Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 17, 2013 Share Posted May 17, 2013 (edited) <?php $prefage_options = ''; $selected_value = false; if (!isset($_SESSION['share_prefage']) || !ctype_digit($_SESSION['share_prefage'])) { // no session value or session value is text $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; } else { //Session value is number. Set selected value $selected_value = $_SESSION['share_prefage']; } for ($i=18; $i<=99; $i++) { $selected = ($i == $selected_value) ? ' selected="selected"' : ''; $prefage_options .= "<option value='{$i}'{$selected}>Around {$i}</option>\n"; } ?> <select name="share_prefage" class="textarea" style="width:150px"> <?php echo $prefage_options; ?> </select> Edited May 17, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
ramone_johnny Posted May 17, 2013 Author Share Posted May 17, 2013 Thank you. Hmm, that looks close, but I lose the "Doesnt Matter" when I make a selection (an age) then go back. I need to keep it there as said, incase they change their mind. Quote Link to comment Share on other sites More sharing options...
ramone_johnny Posted May 18, 2013 Author Share Posted May 18, 2013 Psycho? Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 18, 2013 Share Posted May 18, 2013 And, have you tried to change it as to how you need it? Just look at the code and follow the logic. It should be very simple to make sure "Doesnt matter" is always displayed. I'm not trying to be an ass, just trying to get you to think through the problem. Quote Link to comment Share on other sites More sharing options...
ramone_johnny Posted May 19, 2013 Author Share Posted May 19, 2013 (edited) Thanks man.Sorry, I'm not intending to be lazy - I'm just completely new at PHP.Here's what i ended up with. It seems to work, but I'd say it could be written more efficiently. <?php $prefage_options = ''; $selected_value = false; if (!isset($_SESSION['share_prefage']) || !ctype_digit($_SESSION['share_prefage'])) { // no session value or session value is text $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; } else { $selected_value = $_SESSION['share_prefage']; } for ($i=18; $i<=99; $i++) { $selected = ($i == $selected_value) ? ' selected="selected"' : ''; $prefage_options .= "<option value='{$i}'{$selected}>Around {$i}</option>\n"; } ?> <select name="share_prefage" class="textarea" style="width:150px"> <?php if (isset($_SESSION['share_prefage']) AND ctype_digit($_SESSION['share_prefage'])) { //Session value is number. Set selected value $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; } echo $prefage_options; ?> </select> Here's what I changed/added. <?php if (isset($_SESSION['share_prefage']) AND ctype_digit($_SESSION['share_prefage'])) { //Session value is number. Set selected value $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; } echo $prefage_options; ?> Edited May 19, 2013 by ramone_johnny Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted May 20, 2013 Solution Share Posted May 20, 2013 <?php $selected_value = false; if (isset($_SESSION['share_prefage']) && ctype_digit($_SESSION['share_prefage'])) { //Session value is number. Set selected value $selected_value = $_SESSION['share_prefage']; } $prefage_options = "<option value='Doesnt Matter'>Doesnt Matter</option>\n"; for ($i=18; $i<=99; $i++) { $selected = ($i == $selected_value) ? ' selected="selected"' : ''; $prefage_options .= "<option value='{$i}'{$selected}>Around {$i}</option>\n"; } ?> <select name="share_prefage" class="textarea" style="width:150px"> <?php echo $prefage_options; ?> </select> Quote Link to comment Share on other sites More sharing options...
ramone_johnny Posted May 20, 2013 Author Share Posted May 20, 2013 Ahh, perfect! Thanks dude, appreciate it. 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.