What is the best way to establish a value of a dropdown from the database [closed]

0

I have a dropdown of blood type, and the values are hardcode, but when entering the interface, which is the best way to set the value previously saved from the database, I'm doing something like this:

<select class="selectpicker" id="'.$key.'" name="'.$key.'">
                        <optgroup label="Grupo A">';
                            $sel = ($value == 'A+') ? 'selected' : '' ;
                            echo '<option value="A+" '.$sel.'>A positivo</option>';
                            $sel = ($value == 'A-') ? 'selected' : '' ;
                            echo '<option value="A-" '.$sel.'>A negativo</option>
                        </optgroup>
                        <optgroup label="Grupo B">';
                            $sel = ($value == 'B+') ? 'selected' : '' ;
                            echo '<option value="B+" '.$sel.'>B positivo</option>';
                            $sel = ($value == 'B-') ? 'selected' : '' ;
                            echo '<option value="B-" '.$sel.'>B negativo</option>
                        </optgroup>
                        <optgroup label="Grupo AB">';
                            $sel = ($value == 'AB+') ? 'selected' : '' ;
                            echo '<option value="AB+" '.$sel.'>AB positivo</option>';
                            $sel = ($value == 'AB-') ? 'selected' : '' ;
                            echo '<option value="AB-" '.$sel.'>AB negativo</option>
                        </optgroup>
                        <optgroup label="Grupo O">';
                            $sel = ($value == 'O+') ? 'selected' : '' ;
                            echo '<option value="O+" '.$sel.'>O positivo</option>';
                            $sel = ($value == 'O-') ? 'selected' : '' ;
                            echo '<option value="O-" '.$sel.'>O negativo</option>
                        </optgroup>
                        </select>

What is the best way to put the "selected" attribute to an option of a select with a value from a database?

    
asked by Fernando Garcia 18.05.2018 в 19:11
source

2 answers

1

Although you will have the hard-coded values, you can store them in an array and iterate it so that it is easier to modify and read, besides using interpolation of strings instead of concatenations to write less code:

<?php

    $grupos = array("Grupo A"  => array( "A+" => "A positivo", "A-" => "A negativo"),
                    "Grupo B"  => array( "B+" => "B positivo", "B-" => "B negativo"),
                    "Grupo AB" => array( "AB+" => "AB positivo", "AB-" => "AB negativo"),
                    "Grupo O"  => array( "O+" => "O positivo", "O-" => "O negativo")
                    );

    echo "<select class='selectpicker' id='$key' name='$key'>";

    foreach( $grupos as $grupo => $tipos){

        echo "<optgroup label='$grupo'>";

        foreach( $tipos as $tipo => $descripcion){

            $sel = ($value == $tipo) ? "selected " : "";

            echo "<option value='$tipo' $sel>$descripcion</option>";
        }

        echo "</optgroup>";
    }

    echo "</select>";
?>
    
answered by 18.05.2018 / 20:22
source
0

First you declare your combo and you consult the Database and when you fill it with values you can validate if the value of the BD matches the value of your combo.

<?php 

    $sql= "select id,campo_A from tablaSangre= id=21";

        $ress = mysqli_query($Prueba, $sql);   
                  ?>
    <select class="form-control form-control-lg"  id="combo<?php echo $row['id']; ?>" disabled>
    <?php  while($rows = mysqli_fetch_assoc($ress)){

        if ($row['id']==$rows['id']) {
            echo " <option id='".$rows[campo_A']."' value='".$rows['campo_A']."' selected>".$rows['campo_A']."</option> ";
        }
        else {        
            echo " <option id='".$rows['campo_A']."' value='".$rows['campo_A']."' >".$rows['campo_A']."</option> ";
            }} ?>       
        </select><br>

  </div>

Best regards,

    
answered by 18.05.2018 в 20:02