How to select specific value in drop down list menu input of form

Discussion in 'PHP' started by globalcashsite, Dec 16, 2009.

  1. #1
    Guys

    I am trying to create a data editing form. For example I will enter "email address" and script will get record of that email address from database and show values in fields for edition.

    The problem is one field is drop down list field like

    I wish that script automatically select that value which is taken from database not always show FIRST value "WORKING".

    Above is just example otherwise I have another field that has 6 options.

    Please help me.


    GCS
     
    globalcashsite, Dec 16, 2009 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    <option>Working</option><option selected>Faulty</option></select>  
    Code (markup):
    Option Faulty will show as selected
     
    MyVodaFone, Dec 16, 2009 IP
  3. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But what if value stored in database is "Working", then at the time of editing script should show "Working" by default.

    In your way "Faulty" will be selected/shown permanently no matter what value is in database. :) ?

    GCS
     
    globalcashsite, Dec 16, 2009 IP
  4. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #4
    If I understand correctly, then all you need to do is compare the current dbase value with each SELECT value as you generate them, and also echo "selected" if they are the same. You SHOULD be producing select dropdowns in PHP with a loop, I guess, so something like this (taken from some old code of mine, never mind about the rest of it, it's more complicated than what you need! Just look at the bit that generates the HTML at the end):

    for ($i = 0; $i < sizeof($locals); $i++){
        $menuPID = $locals[$i][3];
        if ($menuPID) {
        echo "<option value='" . $menuPID . "'";
        if ( $WBOptions['placement_id'] == $menuPID ){
            echo "selected"; 
        }
        echo ">" . $CountryName . "</option>";}
    }
    ?>
    Code (markup):
    The code echoes "<option value="... bla bla for each option and if my current "placement_id'" (which is my currently selected value) is equal to the current ID in the loop (which is the current dbase value, stored in the variable $menuPID), then "selected" is also echoed. And that's it. Was that what you were trying to do?
     
    Last edited: Dec 16, 2009
    markowe, Dec 16, 2009 IP
  5. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #5
    If my <select> looked like:

    <select>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    </select>
    HTML:
    And the code that output that was:

    $array = array(
    "1" => "One",
    "2" => "Two",
    "3" => "Three"
    );
    
    function create_select($input)
    {
       echo "<select>" . "\n";
    
       foreach($input as $value => $option)
       {
          echo "<option value='$value'>$option</option>" . "\n";
       }
    
       echo "</select>";
    }
    PHP:
    I'd edit create_select() to take a second parameter, like so:

    function create_select($input, $selected)
    {
       echo "<select>" . "\n";
    
       foreach($input as $value => $option)
       {
          if ($selected == $value) { $str = " selected='selected'"; } else { $str = ""; };
          echo "<option ".$str."value='$value'>$option</option>" . "\n";
       }
    
       echo "</select>";
    }
    
    create_select($array, 2); // Would create the selector with 2/Two "selected"
    
    PHP:
     
    Wogan, Dec 16, 2009 IP
  6. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #6
    Right, Wogan, similar approach to me. In fact, can't think how else you WOULD do it :) Though when I was a relative PHP n00b I'm sure I did it some nasty way!
     
    markowe, Dec 16, 2009 IP
  7. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #7
    this is the logic

    
    $result // from mysql query
    
    $options // array for options, can be fecth from query too
    
    $out .= '<select>';
    
    foreach($options as $option)
    {
    
    if($option->value == $result->value)
    {
    $out .=  '<option value="'.$option->key.'" selected="selected">'.$option->value.'</option>';
    }
    else
    {
    $out .= '<option value="'.$option->key.'">'.$option->value.'</option>';
    }
    }
    
    $out .= '</select>';
    
    
    PHP:
    that just logic :)

    regards
     
    ghprod, Dec 19, 2009 IP
  8. globalcashsite

    globalcashsite Peon

    Messages:
    806
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi guys,

    Thanks for everybody for replying and helping me but I am sorry to say that I am confused. I have to options "Working" and "Faulty", if you can type any code for this, I will be thankful. Here is input

    Select Options: Working, Faulty
    Database Retrieved Variable: $dbselect
    Input field name: $emailstatus

    In reference to above information, if you can give me any code that I apply to my php file I will be thankful. :)

    GCS
     
    globalcashsite, Dec 19, 2009 IP