Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
Cómo seleccionar una opción específica con condición de la base de datos en HTML y PHP

Tengo un problema al codificar el siguiente formulario:

 <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>

Lo que quiero hacer es: conectarme a la base de datos, obtener los datos de allí (si Highschool, entonces = 1; Si Uni, entonces = 2). Quiero que se seleccione la opción para Highschool si la base de datos devuelve 1 y Uni si devuelve 2, pero no sé cómo. :(

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

No estoy muy seguro de lo que estás tratando de hacer... Pero supongo que es algo similar a esto.

 <?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 Report

0

podrías cambiar esto

 <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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!