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

340
Views
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 answers
Answer question

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 Report

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