edcoder
edcoder

Reputation: 533

PHP: How to display select options if no value from database

I am trying to display my dropdown with a value from the database, but if the value is null I want it to show my options.

Currently it keeps showing me the blank select option.

<select class="form-control col-sm-5" id="freqlevels" name="freqlevels" value="<?php if ($customerinfo['freqlevel']) { echo h($customerinfo['freqlevel']);} else { echo "" ; } ?>"">
    <option value=""></option>
    <option value="Twice Weekly">Twice Weekly</option>
    <option value="Weekly">Weekly</option>
    <option value="Fortnightly">Fortnightly</option>
    <option value="Monthly">Monthly</option>
</select>

Please can you suggest what I should do?

Upvotes: 1

Views: 1321

Answers (2)

treyBake
treyBake

Reputation: 6560

You need to make use of conditional statements.

<select name="something" id="my-select">
    <option value="0">Everyone can see me</option>

    <?php if (empty($array['some_key'])) : ?>
        <option value="1">I'm only if some_key is empty</option>
        ..etc..
    <?php endif; ?>
</select>

Then you can check values against the option value:

<option value="<?php echo $key; ?>"
        <?php echo ($key === $_POST['some_key'] ? 'selected' : ''); ?>>
    Hello, world
</option>

Upvotes: 0

vivek modi
vivek modi

Reputation: 497

put your condition outside the value

<?php if ($customerinfo['freqlevel']) { echo value="$customerinfo['freqlevel']";}

hope this will resolve your problem

Upvotes: 1

Related Questions