Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

341
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda