runnerjp Posted February 22, 2008 Share Posted February 22, 2008 im tryin to use <select class="input" id="gender" name="gender"> <option value="<?php echo $gender ?>" selected="selected"></option> <option value="Male" >Male</option> <option value="Female" >Female</option> </select> [code] so the users gender they selected will show but it fails to do so ? [/code] Quote Link to comment Share on other sites More sharing options...
thebadbad Posted February 22, 2008 Share Posted February 22, 2008 Think you'll need to echo it inside the option tags aswell (for it to show): <select class="input" id="gender" name="gender"> <option value="<?php echo $gender ?>" selected="selected"><?php echo $gender ?></option> <option value="Male" >Male</option> <option value="Female" >Female</option> </select> But what's with the second and third option? Do you want the dropdown to automatic select the user specified gender stored in $gender, giving the user the ability to change it to the other option? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 22, 2008 Share Posted February 22, 2008 what you are looking for is this: <select class="input" id="gender" name="gender"> <option value="Male" <?php if($gender == 'Male') echo 'selected'; ?>>Male</option> <option value="Female" <?php if($gender == 'Female') echo 'selected'; ?>>Female</option> </select> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 humm that worked grate lol so if i where to do same with <select id="day" class="input" name="DateOfBirth_Day"> <option value="<?php echo $day ?>">- Day - </option> <option value="1">01</option> <option value="2">02</option> <option value="3">03</option> <option value="4">04</option> <option value="5">05</option> <option value="6">06</option> <option value="7">07</option> <option value="8">08</option> <option value="9">09</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> i would have to do <option value="29" <?php if($day == '29') echo 'selected'; ?>>29</option> <option value="30"<?php if($day == '30') echo 'selected'; ?>>30</option> <option value="31"<?php if($day == '31') echo 'selected'; ?>>31</option> or is there a faster way lol Quote Link to comment Share on other sites More sharing options...
revraz Posted February 22, 2008 Share Posted February 22, 2008 For that you would be better off using a loop, so it only takes a few lines of code instead of 31 lines. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2008 Share Posted February 22, 2008 <select id="day" class="input" name="DateOfBirth_Day"> <?php for ($optVal=1; $optVal<32; $optVal++) { $selected = ($day==$optVal)?' selected="selected"':''; echo "<option value=\"{$optVal}\"{$selected}>" . printf('%02s', $optVal) . "</option>"; } ?> </select> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 lol was attpenting to do 1 and mj poted 1... problem with this is it only displayes 2's repeatedly :S so i can only select 2 2 2 2 2 not 1 2 3 4 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2008 Share Posted February 22, 2008 Hmm, I don't use printf much, this uses str_pad echo "<option value=\"{$optVal}\"{$selected}>" . str_pad($optVal, 2, '0', STR_PAD_LEFT) . "</option>"; Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 that works by displaying the number but does not display the previous chosen value Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 ok iv gone for bit more messing around and now have <p> <label for="birthmonth">Birth Month:</label> <select name="birthmonth" id="birthmonth"> <?php for($i = 1; $i <= 12; $i++) { echo ($i < 10) ? '<option value="0'.$i.'">0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>'; } ?> </select> <label for="birthday">Birth Day:</label> <select name="birthday" id="birthday"> <?php for($i = 1; $i <= 31; $i++) { echo ($i < 10) ? '<option value="0'.$i.'">0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>'; } ?> </select> <label for="birthyear">Birth Year:</label> <select name="birthyear" id="birthyear"> <?php for($i = 1995; $i >= 1900; $i--) { echo '<option value="'.$i.'">'.$i.'</option>'; } ?> </select> </p> but im unsure on how to display the current values selcted by the user in the db ?? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2008 Share Posted February 22, 2008 Change $prevSelectedbirthmonth withthe default value you want. <?php for($i = 1; $i <= 12; $i++) { $selected = ($prevSelectedbirthmonth==$i)?' selected="selected"':''; echo ($i < 10) ? '<option value="0'.$i.'"'.$selected.'>0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>'; } ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 i did <label for="birthmonth">Birth Month:</label> <select name="birthmonth" id="birthmonth"> <?php for($i = 1; $i <= 12; $i++) { $selected = ($month==$i)?' selected="selected"':''; echo ($i < 10) ? '<option value="0'.$i.'"'.$selected.'>0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>'; } ?> </select> <label for="birthday">Birth Day:</label> <select name="birthday" id="birthday"> <?php for($i = 1; $i <= 31; $i++) { echo ($i < 10) ? '<option value="0'.$i.'">0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>'; } ?> </select> <label for="birthyear">Birth Year:</label> <select name="birthyear" id="birthyear"> <?php for($i = 1995; $i >= 1900; $i--) { echo '<option value="'.$i.'">'.$i.'</option>'; } ?> </select> </p></td> </tr> and did not work Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 . Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2008 Share Posted February 22, 2008 Didn't realize you already had an IF statemetn in that code. But, it would be very helpful if you explained what was happening instead of just saying "It didn't work" <?php for($i = 1; $i <= 12; $i++) { $selected = ($month==$i)?' selected="selected"':''; echo "<option value=\"0$i\"$selected>0$i</option>"; } ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 humm it still does not display the previous day... would i possible need to add something like <?php echo $month ?> Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 shud repeat what im doing to help a little... a user seltect there dob so 8/08/2000 and when then come back to the update page i want their dob to display 8/08/2000 in the dob menu so if they dont need to change this this dont have too Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2008 Share Posted February 22, 2008 Again, don't just state that "it didn't work" give specifics of what IS happening. Is the select list being generated successfully? If so, have you checked the generated HTML output to see if "selected" is in the code, but maybe not formatted correctly? How are you setting the variable for the default value $month? Have you verified that it holds a value? Were not mind readers. Perhaps the $month value has the leading '0' in which case '08' != '8'. This is a very simple problem and should not need two pages on the forum to solve. Quote Link to comment Share on other sites More sharing options...
runnerjp Posted February 22, 2008 Author Share Posted February 22, 2008 hummm ok i have been thinkin on the subject lol and have relized a fault of mine.... why would someone need to chnage their birthday ??? lol as this will stay the same.. so all this trouble and its not really needed but i would still like to know where its going wrong so if i select 07 as the date 09 month and 1995 as year then the output is 07-09-1995 that works fine in batabase and everything but when i go back to the update field where i have an example of <textarea class="input" id="about_me" name="about_me" rows="4" cols="40"><?php echo $about_me ?></textarea> where the text allready added by the users is shown but the date fields reset themselfs to 01-01-1995 i want it to show 07-09-1995 is that bttr ?? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2008 Share Posted February 22, 2008 Well, you still didn't answer my questions above as to whether you have done some of that debugging. I would start from the beginning. First check the "default" values received from the query to make sure you are setting those values correctly. If they are check to see if they are being interpreted correctly in the comparison. If so, then determine what is wrong. If I run this code, the select list is getting set to the correct default value. <select name="month"> <?php $month = '3'; for($i = 1; $i <= 12; $i++) { $selected = ($month==$i)?' selected="selected"':''; echo ($i < 10) ? '<option value="0'.$i.'"'.$selected.'>0'.$i.'</option>' : '<option value="'.$i.'">'.$i.'</option>'; } ?> </select> So, your problem is most likely that $month is not getting set properly OR the value of $month has the zero padding on it. (e.g. '08' != '8') 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.