0

I want to have a drop-down list have a default value akin to how you would have value="abc" in a text field or similar. The only caveat is that I would like it to be defaulted to where an SQL query points it to. Excuse the long code.

//prior code where table and `foreach()` loop begins
                        <td>
                            <input type="text"
                                value="<?php echo $var["author"]; ?>"
                                required="required">
                            </input>
                        </td>
                        <td>
                            <select name="condition"
                                value="<?php echo $var["condition"]; ?>"
                                    <option>M</option>
                                    <option>NM</option>
                                    <option>E</option>
                                    <option>G</option>
                                    <option>P</option>
                            </select>
                        </td>
//subsequent code where table is closed

For the first half, I have a text field of default value $var["author"], because that is what I query before beforehand. For the second, I can't seem to get the same result, due to it being a drop-down menu instead of a text field. If the .sql query brings up "NM", the default value will be "M", always. Any way to do this?

1 Answer 1

2

What you want is this:

//prior code where table and `foreach()` loop begins
                        <td>
                            <input type="text"
                                value="<?php echo $var["author"]; ?>"
                                required="required">
                            </input>
                        </td>
                        <td>
                            <select name="condition">
                                    <option value="M"<?php echo ($var["condition"] == 'M' ? ' selected="selected"' : ''); ?>>M</option>
                                    <option value="NM"<?php echo ($var["condition"] == 'NM' ? ' selected="selected"' : ''); ?>NM</option>
                                    <option value="E"<?php echo ($var["condition"] == 'E' ? ' selected="selected"' : ''); ?>E</option>
                                    <option value="G"<?php echo ($var["condition"] == 'G' ? ' selected="selected"' : ''); ?>G</option>
                                    <option value="P"<?php echo ($var["condition"] == 'P' ? ' selected="selected"' : ''); ?>P</option>
                            </select>
                        </td>
//subsequent code where table is closed

or more elegant:

$dropdownOptions = array('N', 'NM', 'E', 'G', 'P');
//prior code where table and `foreach()` loop begins
                        <td>
                            <input type="text"
                                value="<?php echo $var["author"]; ?>"
                                required="required">
                            </input>
                        </td>
                        <td>
                            <select name="condition">
                                    <?php foreach ($dropdownOptions AS $option) {
                                         echo '<option value="' . $option . '"' . ($var["condition"] == $option ? ' selected="selected"' : '') . '>' . $option . '</option>';
                                    } ?>
                            </select>
                        </td>
//subsequent code where table is closed
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.