Pull Down Menu

Discussion in 'PHP' started by newbie8899, Dec 18, 2007.

  1. #1
    Hi all.. i m not sure shud i post this question to html forum or php as it needs the php to select the value from the db. so i decide to post it here.

    I have a problem with the <select></select> stuff. my problem is like this.

    If i have a set of pull down menu using <select></select> the values in it are:
    orange, apple and banana.

    at the beginning, i will pull out the value from the db.. for example, if the value pull out from the db table is banana. so i want my <select></select> in the form to select the banana instead of keep selecting the 1st item.. how am i going to do this?

    thanks a lot
     
    newbie8899, Dec 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $values = array('orange', 'apple', 'banana');
    
    echo '<select name="foo">';
    foreach ($values AS $value)
    {
        $selected = (isset($_POST['foo']) AND $_POST['foo'] == $value)
            ? ' selected="selected"'
            :   null;
    
        echo "<option value=\"{$value}\"{$selected}>{$value}</option>\n";
    }
    echo "</select>\n";
    
    PHP:
     
    nico_swd, Dec 19, 2007 IP
  3. newbie8899

    newbie8899 Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks nico:eek:
     
    newbie8899, Dec 23, 2007 IP