PHP Option Dropdown set Option to One Stored in DB

前端 未结 2 1547
忘了有多久
忘了有多久 2021-01-26 11:42

I have a PHP form which I use to edit a record in the MySQL data base and it has a drop down list of values I would like to set the option that is held in the database to be the

相关标签:
2条回答
  • 2021-01-26 11:50

    You could use

    while($row = mysql_fetch_assoc($result)){
        $selected = "";
        if($row['id'] == $curValue){
            $selected = "selected=\"selected\"";
        }
        echo "<option value=\"{$row['id']}\" $selected>{$row['name']}</option> 
    }
    

    After you've updated your question, it seems MattP's answer suits your situation better.

    0 讨论(0)
  • 2021-01-26 12:05

    If it's a small dropdown this is quick and easy

    <label>Type: </label>
                    <?php
                        if(isset($item->type_id))
                            $tid = $item->type_id;
                        else
                            $tid = 1; // Set this to your preferred default
                    ?>
                    <select name="type">
                        <option value="1" <?php if($tid == 1) echo ' selected' ?>>Type 1</option>
                        <option value="2" <?php if($tid == 2) echo ' selected' ?>>Type 2</option>
                        <option value="3" <?php if($tid == 3) echo ' selected' ?>>Type 3</option>
                    </select>
    

    If it's a bigger dropdown write a php function to create the dropdown using an iterative approach

    EDIT * is this right as it is now defaulting to the $tid

                            <?php
                                    if(isset($contact['Region']))
                                        $tid = $contact['Region'];
                                    else
                                        $tid = 'South East'; // Set this to your preferred default
                                ?>          
                                <select id="inputRegion" name="inputRegion">
                                        <option name="inputRegion" id="inputRegion" value="Scotland" <?php if($tid == 'Scotland') echo ' selected' ?>>Scotland</option>
                                        <option name="inputRegion" id="inputRegion" value="North West" <?php if($tid == 'North West') echo ' selected' ?>>North West</option>
                                        <option name="inputRegion" id="inputRegion" value="North East" <?php if($tid == 'North East') echo ' selected' ?>>North East</option>
                                        <option name="inputRegion" id="inputRegion" value="Yorkshire and Humberside" <?php if($tid == 'Yorkshire and Humberside"') echo ' selected' ?>>Yorkshire & Humberside</option>
                                        <option name="inputRegion" id="inputRegion" value="East Midlands" <?php if($tid == 'East Midlands') echo ' selected' ?>>East Midlands</option>
                                        <option name="inputRegion" id="inputRegion" value="East Anglia" <?php if($tid == 'East Anglia') echo ' selected' ?>>East Anglia</option>
                                        <option name="inputRegion" id="inputRegion" value="London" <?php if($tid == 'London') echo ' selected' ?>>London</option>
                                        <option name="inputRegion" id="inputRegion" value="South East" <?php if($tid == 'South East') echo ' selected' ?>>South East</option>
                                        <option name="inputRegion" id="inputRegion" value="South West" <?php if($tid == 'South West') echo ' selected' ?>>South West</option>
                                        <option name="inputRegion" id="inputRegion" value="Wales" <?php if($tid == 'Wales') echo ' selected' ?>>Wales</option>
                                        <option name="inputRegion" id="inputRegion" value="West Midlands" <?php if($tid == 'West Midlands') echo ' selected' ?>>West Midlands</option>
                                        <option name="inputRegion" id="inputRegion" value="Northern Ireland" <?php if($tid == 'Northern Ireland') echo ' selected' ?>>Northern Ireland</option>
                                </select>
    
    0 讨论(0)
提交回复
热议问题