SELECT Dynamic and option selected by default

0

I am completely new and without any experience, I indicate: As shown in the image I already made the SQL query so that the countries from the database are loaded to the SELECT of my page, now I want to leave selected by default a Country (for example, ECUADOR) how do I make this country selected in the cycle? Thanks (html5, php)

    
asked by dmlisto 18.06.2017 в 01:12
source

2 answers

0

change:

<option selected disabled>seleccione un pais</option>

for

<option selected>Ecuador</option>

<select>
  <option selected>Ecuador</option>
  <option>Rusia</option>
</select>
    
answered by 18.06.2017 в 02:26
0

Based on your code and using an alternative notation for the while

<?php
$sql_consulta = 'SELCT * FROM pais ORDER BY nombre';
$consulta = mysqli_query($cone, $sql_consulta) or die(mysqli_errno());
?>
<select name="pais" id="pais">
    <option disabled>Selecciona un Pais</option>
    <?php while ($fila = $consulta->fetch_assoc()): ?>
        <?php $atributo = ($fila['nombre'] == $nombre) ? 'selected' : '' ?>
        <option <?= $atributo ?> ><?= $fila['nombre'] ?></option>
    <?php endwhile; ?>
</select>

The line that does everything is this

<?php $atributo = ($fila['nombre'] == $nombre) ? 'selected' : '' ?>

since if the name value of the row is the name you are looking for it assigns 'selected' otherwise it assigns empty.

    
answered by 19.06.2017 в 03:10