Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

339
Vistas
How to select specific option w/ condition from Database in HTML and PHP

I'm haveing a problem when coding the following form:

       <select  value="" required>
         <option value = "0" >Choose Organization </option>
         <option value="1" ' . $var1 . ' >Highschool</option>
         <option value="2" ' . $var2 . '>University</option>
       </select>
       <label>Type of Organization</label>

What I want to do is: connect to the database, get the data from there (if Highschool, then = 1; If Uni, then = 2). I want the option to be selected for Highschool if the database returns 1 and Uni if it returns 2, but I do not know how. :(

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Not really sure what you're trying to do... But I'm assuming is something similar to this?

<?php 

function getDB() {
  $host = "localhost";
  $user = "root";
  $password = "pass";
  $dbname = "schools";

  return new PDO(
    sprintf(
      'mysql:host=%s;dbname=%s;charset=utf8',
      $host,
      $dbname
    ),
    $user,
    $password
  );
}

$db = getDB();

$stmt = $db->prepare('SELECT * FROM `database` WHERE value1 = :value1');
$stmt->bindValue(':value1', $value1);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<select value="" required>
  <?php 
  foreach ($data as $value) {
    if($value == "Highschool") {
      echo '<option value="1">Highschool</option>';
    }
    if($value == "University") {
      echo '<option value="2">University</option>';
    }
  }
   ?>
   </select>
   <label>Type of Organization</label>
over 4 years ago · Santiago Trujillo Denunciar

0

you could change this

<select  value="" required>
 <option value = "0" >Choose Organization </option>
 <option value="1" ' . $var1 . ' >Highschool</option>
 <option value="2" ' . $var2 . '>University</option>
</select>
<label>Type of Organization</label>
to 
<select name="name" class="form-control">
    <?php
    include('config_file.php');
    $result = mysql_query("SELECT * FROM table");
    while($row = mysql_fetch_array($result))
        {
            echo '<option value="'.$row['id'].'">';
            echo $row['route'].'  :'.$row['type'].'  :'.$row['time'];
            echo '</option>';
        }
    ?>
</select>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda