editing drop down menu

Discussion in 'PHP' started by apple, Apr 25, 2007.

  1. #1
    suppose i make a dropdown menu having value 1, 2, 3, 4, 5, one option can be selected, and selected option is stored in database.
    now i create an edit page, how do i display the selected value in the menu and other values in dropdown,
    for example,
    Menu is like
    Select One
    1
    2
    3
    4
    5

    and i select 3, this 3 is stored in database, now on the edit page i want to show 3 as already selected,
    something like that..
    <select name="select_thing">
    	<option value="1">1</option>
    	<option value="2">2</option>
    	<option value="3" selected>3</option>
    	<option value="4">4</option>
    	<option value="5">5</option>
    </select>
    HTML:
    I will be very thankful, if someone could help by explaining how do i make option 3 as selected.. eg. how i know option 3 is selected and stored in db.
     
    apple, Apr 25, 2007 IP
  2. reza56

    reza56 Active Member

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #2
    Fetch rows of table to an array and use a foreach for displaying them. like this :

    foreach($rows as $row) 
      echo "<option value='{$row[name]}'>{$row[name]}</option>";
    PHP:
     
    reza56, Apr 25, 2007 IP
  3. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #3
    urm, did you actually read that post ???

    @op can you post the code that draws the form to begin with ...

    it's quite easy only I can't guess your database schema
     
    krakjoe, Apr 26, 2007 IP
  4. dzysyak

    dzysyak Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here is some code (hope I understand your question):

    
    
    ?><select name="select_name"><?
    $db->query("SELECT * FROM users WHERE id={$_SESSION['user']['ID']}");
    if($db->next_record()) for($i=1;$i<7;$i++){
    ?>option value="<?=$i;?>" <?=($db->f('value')==$i)?:"selected":"";?>><?=$i;?></option><?
    }
    ?></select><?
    
    
    PHP:
    This is quite abstract example, hope it is clear and helpful.
     
    dzysyak, Apr 26, 2007 IP