retrieving data into a drop down menu...

Discussion in 'PHP' started by alhen, Oct 12, 2007.

  1. #1
    Hi,
    I can get my data from the mysql database into a drop down menu ok, but it always shows what's chosen, then the entire list below it. Like this:

    (if the menu is to choose a year)

    
    <select name="ud_year" tabindex="190" id="year">
       <option value="<? echo $year;?>" selected><? echo $year;?></option>
       <option value="2007" selected>2007</option>
       <option value="2008" selected>2008</option>
       <option value="2009" selected>2009</option>
       <option value="2010" selected>2010</option>
    </select>
    
    Code (markup):
    But if the year already stored in the data base is, for example, 2009 - the menu will display like this when you drop it down:

    2009
    2007
    2008
    2009
    2010

    with two 2009's.

    is there a way for it to pull 2009 from mysql, but just have it show 2009 in the list, in place?

    I hope this makes sense, please let me know if you need clarification.

    Thanks! ~Alex
     
    alhen, Oct 12, 2007 IP
  2. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <select name="ud_year" tabindex="190" id="year">
       <option value="2007"<? if ($year == 2007) echo " selected"; ?>>2007</option>
       <option value="2008"<? if ($year == 2008) echo " selected"; ?>>2008</option>
       <option value="2009"<? if ($year == 2009) echo " selected"; ?>>2009</option>
       <option value="2010"<? if ($year == 2010) echo " selected"; ?>>2010</option>
    </select>
    
    Code (markup):
     
    tamen, Oct 12, 2007 IP