Select option selected

-1

I need to leave the selected option selected according to the field recorded in mysql. The form is to edit pos so it displays the available fields with the foreach.

Ex:

 <select name="segurado"  >
    <option value=""></option>
    <?php foreach ($result as $rs) { ?>
        <option value="<?php echo $rs["nome"]; ?>"><?php echo $rs["nome"]; ?></option>
    <?php } ?>
</select> 

In this way select shows the insured table fields but I want to leave the selected record field for selected edit.

Maybe I did not make myself understood correctly: I retrieve the field saved in mysql and I can put it in the select label but this is not the solution. But this shows that the variable to compare is correct, ie the one recorded in the table when registering. Since I want to edit the registry, I need this saved and saved field to be set to selected in options, which in turn is being fed by foreach and creating the elements in the same way as in the registry. Here is the example of how to fill in the option:

                            $sql = "SELECT * FROM seguradoras ORDER BY nome";
                            try {
                                $stmt = $DB->prepare($sql);
                                $stmt->execute();
                                $results = $stmt->fetchAll();
                            } catch (Exception $ex) {
                                echo($ex->getMessage());
                            }

With it populated, I want it to show the variable that comes from the bank in the case $ residential-> insurance

Improved people?

    
asked by anonymous 11.08.2017 / 11:29

4 answers

0

When you meet the condition you want to select put <option selected>

<select name="segurado"  >
    <option value=""></option>
    <?php foreach ($result as $rs) { ?>
        <option value="<?php echo $rs["nome"]; ?>" <?php if($rs["minha_opcao_selecionada"] === true ){ echo "selected"}  ?>  ><?php echo $rs["nome"]; ?></option>
    <?php } ?>
</select>
    
11.08.2017 / 12:49
0
Assuming you supply your form with with entries in the table you could have a specific field for this type select with on or off or by checking the item itself examples:

<?php
    // sua lógica de acesso ao banco...
?>
<select name="segurado">
    <option value=""></option>
    <?php 
        foreach ($result as $rs) {
            // caso use um campo especifico
            if ( $rs['select'] === 'on' ) {
                echo '<option value="' . $rs['nome'] . '" selected="true">' . $rs['nome'] .'</option>';
            } else {
                echo '<option value="' . $rs['nome'] . '">' . $rs['nome'] .'</option>';
            }
            // ou pelo item mesmo
            if ( $rs['nome_do_item'] ) {
                echo '<option value="' . $rs['nome'] . '" selected="true">' . $rs['nome'] .'</option>';
            } else {
                echo '<option value="' . $rs['nome'] . '">' . $rs['nome'] .'</option>';
            }
        }
    ?>
</select> 
    
11.08.2017 / 13:00
0

If you want a compact option here you have one:

<select name="segurado">
    <option value=""></option>
    <?php foreach ($result as $rs): ?>
        <option value="<?=$rs['nome']?>" <?= ($rs['select'] == 'on') ?  'selected' : '' ?> ><?= $rs['nome'] ?></option>
    <?php endforeach; ?>
</select> 
    
11.08.2017 / 15:28
0

Well, changing the code and replacing the variables finally got the result I wanted:

<?php foreach ($results as $rs) { ?>
    <option value="<?php echo $rs["nome"]; ?>" <?php if ($residencial->seguradora == $rs["nome"] ) { echo "selected";}  ?>  ><?php echo $rs["nome"]; ?></option>
<?php } ?>

Thank you very much and a big hug!

    
11.08.2017 / 23:05