简体   繁体   中英

How to SET a heading for the select dropdown that query data from the database

I'm wondering if it is possible to SET a heading (ie Select Name)for the select dropdown that query data from the database. Many thanks

Current code:

<select>
<?php 
include("db.php";)
          $sql = SELECT * FROM Persons;
          $result = mysqli_query($con,$sql);
          while ($row = mysqli_fetch_assoc($result)) {
          echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";}?>
           </select><br>

Desired effect

<select>
     <option value="default">Select Name</option>
         <option value="Tom">Tom</option>
     <option value="Mary">Mary</option>
     <option value="John">John</option>
           </select><br>

Just prepend a <option value="default"> to the other options:

<select>
  <option value="default">Select Person</option> <!-- default option -->
  <?php 
  include("db.php";)
  $sql = "SELECT * FROM Persons";
  $result = mysqli_query($con,$sql);
  while ($row = mysqli_fetch_assoc($result)) {
    echo "  <option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
  }
  ?>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM