Quick question....

Discussion in 'PHP' started by karl_murphy, Jun 10, 2008.

  1. #1
    Does anyone know how to set the default value (displayed value) of a drop down menu to that of a value from a database? I've tried the following, but "DD" is always displayed as the default value:

    <select name = "select_day" value = <?php echo $select_day; ?> />
    							<option value = "DD">DD</option> 
    							<option value = "01">01</option>
    							<option value = "02">02</option>
    							<option value = "03">03</option>
    							<option value = "04">04</option>
    							<option value = "05">05</option>
    							<option value = "06">06</option>
    							<option value = "07">07</option>
    							<option value = "08">08</option>
    							<option value = "09">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>
    HTML:
    Any ideas?

    Regards
     
    karl_murphy, Jun 10, 2008 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    add selected=selected to the option wich must be selected :)
     
    EricBruggema, Jun 10, 2008 IP
  3. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    The value that should be selected and displayed is contained within the variable $select_day

    That why I've set the value to this, but it doesn't seem to work.
     
    karl_murphy, Jun 10, 2008 IP
  4. softvision

    softvision Peon

    Messages:
    146
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Make sure that the value contained in $select_day is the vales displayed in dropdown list.
     
    softvision, Jun 11, 2008 IP
  5. skyfe

    skyfe Active Member

    Messages:
    256
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    63
    #5
    Hmm, ofcourse DD is the value now, since the first option contains this value.. So either make the first option having the value of the variable $select_day, or... well, that's actually the only possibility to do this I think. So for example:

    
    <select name = "select_day" />
                                <option value = "<?php echo $select_day; ?>"> Select </option>
                                <option value = "DD">DD</option>
                                <option value = "01">01</option>
                                <option value = "02">02</option>
                                <option value = "03">03</option>
                                <option value = "04">04</option>
                                <option value = "05">05</option>
                                <option value = "06">06</option>
                                <option value = "07">07</option>
                                <option value = "08">08</option>
                                <option value = "09">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>
    
    Code (markup):
    for example.
     
    skyfe, Jun 11, 2008 IP
  6. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #6
    There are two methods for achieving this.

    1. The method that skyfe suggested. In this method, you place the database value as the first option in the select menu. This way, your selected value will appear twice: once in the top and another time in its place in the menu.

    2. Loop through the values inside the <select> and add "selected" before the database value.
    Example:

    <select name="select_day">
    <?  
    for($i=01;$i<32;$i++){
    ?>
    <option value="<?=$i?>" <? if($i == $select_day) echo "checked"; ?>><?=$i?></option>
    <?
    }
    ?>
    </select>
    HTML:
     
    Lordo, Jun 11, 2008 IP
  7. skyfe

    skyfe Active Member

    Messages:
    256
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    63
    #7
    Ah did he mean it like that, yes, then I guess your method is good indeed :). So if it's equal to $select_day it does only show it on the top, and not twice ^^
     
    skyfe, Jun 11, 2008 IP
  8. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #8
    Thank you :) It will not show it in the top, it will make it the default value (as he asked) so when you click the menu, you will find that you are on the selected 10 for example and there are from 1 to 9 above your selection.
     
    Lordo, Jun 11, 2008 IP